Computer Knowledge

Object-Oriented Programming

2,686 Questions

Object-oriented programming questions test core computer science concepts like classes, inheritance, polymorphism, and encapsulation. The focus includes Java program structures, method overloading, and memory allocation for objects. This topic is essential for technical sections in various recruitment tests.

Java class definitionsMethod overriding rulesPolymorphism conceptsGeneric type parametersMemory allocation in objects

Object-Oriented Programming Questions

Multiple choice
  1. 1, 2 and 4

  2. 2, 3 and 5

  3. 3, 4 and 5

  4. 1, 2 and 3

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

sounds correct as in the example below class CoffeeCup {     private int innerCoffee;     public CoffeeCup() {    }       public void add(int amount) {     innerCoffee += amount;    }    //...  } The compiler gives default constructors the same access level as their class. In the example above, class CoffeeCup is public, so the default constructor is public. IfCoffeeCup had been given package access, the default constructor would be given package access as well. (3) is correct. The Java compiler generates at least one instance initialisation method for every class it compiles. In the Java class file, the instance initialisation method is named "<init>." For each constructor in the source code of a class, the Java compiler generates one <init>() method. If the class declares no constructors explicitly, the compiler generates a default no-arg constructor that just invokes the superclass's no-arg constructor. As with any other constructor, the compiler creates an <init>() method in the class file that corresponds to this default constructor. (5) is correct. The compiler creates a default constructor if you do not declare any constructors in your class.

Multiple choice
  1. class Test1 will not compile.

  2. The Test1 hashCode() method is more efficient than the Test2 hashCode() method.

  3. The Test1 hashCode() method is less efficient than the Test2 hashCode()method.

  4. class Test2 will not compile.

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

The so-called "hashing algorithm" implemented by class Test1 will always return the same value, 42, which is legal but which will place all of the hash table entries into a single bucket, the most inefficient setup possible. Option A and D are incorrect because these classes are legal. Option B is incorrect based on the logic described above.

Multiple choice
  1. after line 12

  2. after line 14

  3. after line 7, when doBar() completes

  4. after line 15, when main() completes

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Option B is correct. All references to the Bar object created on line 6 are destroyed when a new reference to a new Bar object is assigned to the variable newBar on line 14. Therefore the Bar object, created on line 6, is eligible for garbage collection after line 14. Option A is wrong. This actually protects the object from garbage collection. Option C is wrong. Because the reference in the doBar() method is returned on line 7 and is stored in newBar on line 12. This preserver the object created on line 6. Option D is wrong. Not applicable because the object is eligible for garbage collection after line 14.

Multiple choice
  1. After line 7

  2. After line 8

  3. After the start() method completes

  4. When the instance running this code is made eligible for garbage collection.

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

Option D is correct. By a process of elimination. Option A is wrong. The variable d is a member of the Test class and is never directly set to null. Option B is wrong. A copy of the variable d is set to null and not the actual variable d. Option C is wrong. The variable d exists outside the start() method (it is a class member). So, when the start() method finishes the variable d still holds a reference.

Multiple choice
  1. Test( )

  2. Test(void)

  3. public Test( )

  4. public Test(void)

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

Option A and B are wrong because they use the default access modifier and the access modifier for the class is public (remember, the default constructor has the same access modifier as the class). Option D is wrong. The void makes the compiler think that this is a method specification - in fact if it were a method specification the compiler would spit it out.

Multiple choice
  1. 0

  2. 1

  3. 2

  4. 3

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

By the time line 8 has run, the only object without a reference is the one generated as a result of line 6. Remember that "Java is pass by value," so the reference variable x is not affected by the m1() method.

Multiple choice
  1. public

  2. private

  3. protected

  4. transient

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

Access modifiers dictate which classes, not which instances, may access features. Methods and variables are collectively known as members. Method and variable members are given access control in exactly the same way. private makes a member accessible only from within its own class protected makes a member accessible only to classes in the same package or subclass of the class default access is very similar to protected (make sure you spot the difference) default access makes a member accessible only to classes in the same package. public means that all other classes regardless of the package that they belong to, can access the member (assuming the class itself is visible) final makes it impossible to extend a class, when applied to a method it prevents a method from being overridden in a subclass, when applied to a variable it makes it impossible to reinitialise a variable once it has been initialised abstract declares a method that has not been implemented. transient indicates that a variable is not part of the persistent state of an object. volatile indicates that a thread must reconcile its working copy of the field with the master copy every time it accesses the variable. After examining the above it should be obvious that the access modifier that provides the most restrictions for methods to be accessed from the subclasses of the class from another package is C - protected. A is also a contender but C is more restrictive, B would be the answer if the constraint was the "same package" instead of "any package" in other words the subclasses clause in the question eliminates default.

Multiple choice
  1. public int method1(int a, int b) {return 0; }

  2. private int method1(int a, int b) { return 0; }

  3. public short method1(int a, int b) { return 0; }

  4. static protected int method1(int a, int b) { return 0; }

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

Option A is correct - because the class that extends A is just simply overriding method1. Option B is wrong - because it can't override as there are less access privileges in the subclass method1. Option C is wrong - because to override it, the return type needs to be an integer. The different return type means that the method is not overriding but the same argument list means that the method is not overloading. Conflict - compile time error. Option D is wrong - because you can't override a method and make it a class method i.e. using static.

Multiple choice
  1. 1 and 2

  2. 2 and 3

  3. 3 and 4

  4. 1 and 5

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

(3) is correct because an abstract class doesn't have to implement any or all of its interface's methods. (4) is correct because the method is correctly implemented ((7 > 4) is a boolean). (1) is incorrect because interfaces don't implement anything. (2) is incorrect because classes don't extend interfaces. (5) is incorrect because interface methods are implicitlypublic, so the methods being implemented must be public.

Multiple choice
  1. The elements in the collection are ordered.

  2. The collection is guaranteed to be immutable.

  3. The elements in the collection are guaranteed to be unique.

  4. The elements in the collection are accessed using a unique key.

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

Yes, always the elements in the collection are ordered.

Multiple choice
  1. It must be marked final.

  2. It can be marked abstract.

  3. It can be marked public.

  4. It can be marked static.

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Option B is correct because a method-local inner class can be abstract, although it means a subclass of the inner class must be created if the abstract class is to be used (so an abstract method-local inner class is probably not useful). Option A is incorrect because a method-local inner class does not have to be declaredfinal (although it is legal to do so). C and D are incorrect because a method-local inner class cannot be made public(remember-you cannot mark any local variables as public), or static.

Multiple choice
  1. protected int a;

  2. transient int b = 3;

  3. private synchronized int e;

  4. volatile int d;

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

Option C will not compile; the synchronized modifier applies only to methods. Option A and B will compile because protected and transient are legal variable modifiers. Option D will compile because volatile is a proper variable modifier.

Multiple choice
  1. Class B'S constructor is public.

  2. Class B'S constructor has no arguments.

  3. Class B'S constructor includes a call to this( ).

  4. None of these.

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Option B is correct. Class B inherits Class A's constructor which has no arguments. Option A is wrong. Class B inherits Class A's constructor which uses default access. Option C is wrong. There is just no implicit call to this( ).

Multiple choice
  1. 1 only

  2. 2 and 5

  3. 3 and 4

  4. 3 and 4

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

(2) and (5). TreeMap is the only class that must be imported. TreeSet does not need an import statement because it is described with a fully qualified name. (1) is incorrect because TreeMap must be imported. (3) is incorrect syntax for an import statement. (4) is incorrect because it will not import TreeMap, which is required.