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. 0

  2. 1

  3. 2

  4. 3

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

This is an example of the islands of isolated objects. By the time line 11 has run, the objects instantiated in lines 6 and 7 are referring to each other, but no live thread can reach either of them.

Multiple choice
  1. 1 and 4

  2. 2 and 5

  3. 3 and 6

  4. 4 and 6

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

(3), (6). Both are legal class declarations. (1) is wrong because a class cannot be abstract and final—there would be no way to use such a class. (2) is wrong because interfaces and classes cannot be marked asstatic. (4) and (5) are wrong because classes and interfaces cannot be marked asprotected.

Multiple choice
  1. You must have a reference to an instance of the enclosing class in order to instantiate it.

  2. It does not have access to nonstatic members of the enclosing class.

  3. It's variables and methods must be static.

  4. It must extend the enclosing class.

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

Option B is correct because a static nested class is not tied to an instance of the enclosing class, and thus can't access the nonstatic members of the class (just as a static method can't access nonstatic members of a class). Option A is incorrect because static nested classes do not need (and can't use) a reference to an instance of the enclosing class. Option C is incorrect because static nested classes can declare and define nonstatic members. Option D is wrong because it just is. There's no rule that says an inner or nested class has to extend anything.

Multiple choice
  1. Runnable r = new Runnable() { };

  2. Runnable r = new Runnable(public void run() { });

  3. Runnable r = new Runnable { public void run(){}};

  4. System.out.println(new Runnable() {public void run() { }});

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

D is correct. It defines an anonymous inner class instance, which also means it creates an instance of that new anonymous class at the same time. The anonymous class is an implementer of the Runnable interface, so it must override the run() method ofRunnable. A is incorrect because it doesn't override the run() method, so it violates the rules of interface implementation. B and C use incorrect syntax.

Multiple choice
  1. Boo f = new Boo(24) { };

  2. Boo f = new Bar() { };

  3. Bar f = new Boo(String s) { };

  4. Boo f = new Boo.Bar(String s) { };

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

Option B is correct because anonymous inner classes are no different from any other class when it comes to polymorphism. That means you are always allowed to declare a reference variable of the superclass type and have that reference variable refer to an instance of a subclass type, which in this case is an anonymous subclass of Bar. Since Bar is a subclass of Boo, it all works. Option A is incorrect because it passes an int to the Boo constructor, and there is no matching constructor in the Boo class. Option C is incorrect because it violates the rules of polymorphism—you cannot refer to a superclass type using a reference variable declared as the subclass type. The superclass is not guaranteed to have everything the subclass has. Option D uses incorrect syntax.

Multiple choice
  1. Inheritance

  2. Polymorphism

  3. Abstraction

  4. Encapsulation

  5. None of the above

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

Object based languages do not support inheritance, which is the fundamental property of the object oriented programming. These languages support all other properties of object oriented programming except inheritance while object oriented languages do support all the features of OOPs.

Multiple choice
  1. Assertions can be enabled or disabled on a class-by-class basis.

  2. Conditional compilation is used to allow tested classes to run at full speed.

  3. Assertions are appropriate for checking the validity of arguments in a method.

  4. The programmer can choose to execute a return statement or to throw an exception if an assertion fails.

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

Option A is correct. The assertion status can be set for a named top-level class and any nested classes contained therein. This setting takes precedence over the class loader's default assertion status, and over any applicable per-package default. If the named class is not a top-level class, the change of status will have no effect on the actual assertion status of any class. Option B is wrong. Is there such a thing as conditional compilation in Java? Option C is wrong. For private methods - yes. But do not use assertions to check the parameters of a public method. An assert is inappropriate in public methods because the method guarantees that it will always enforce the argument checks. A public method must check its arguments whether or not assertions are enabled. Further, the assert construct does not throw an exception of the specified type. It can throw only anAssertionError. Option D is wrong. Because you're never supposed to handle an assertion failure. That means don't catch it with a catch clause and attempt to recover.

Multiple choice
  1. A static method cannot be synchronized.

  2. If a class has synchronized code, multiple threads can still access the nonsynchronized code.

  3. Variables can be protected from concurrent access problems by marking them with the synchronized keyword.

  4. When a thread sleeps, it releases its locks.

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

2 is correct because multiple threads are allowed to enter nonsynchronized code, even within a class that has some synchronized methods. 1 is incorrect because static methods can be synchronized; they synchronize on the lock on the instance of class java.lang. The class that represents the class type. 3 is incorrect because only methods—not variables—can be marked synchronized. 4 is incorrect because a sleeping thread still maintains its locks.

Multiple choice
  1. public

  2. private

  3. protected

  4. default access

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

The only two real contenders are C and D. Protected access Option C makes a member accessible only to classes in the same package or subclass of the class. While default access Option D makes a member accessible only to classes in the same package.

Multiple choice
  1. public static short stop = 23;

  2. protected short stop = 23;

  3. transient short stop = 23;

  4. final void madness(short stop);

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

(A) is valid interface declarations. (B) and (C) are incorrect because interface variables cannot be either protected ortransient. (D) is incorrect because interface methods cannot be final or static.

Multiple choice
  1. 1 and 4

  2. 2 and 3

  3. 1 and 3

  4. 2 and 4

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

(2) is correct because class type Ticker is part of the class hierarchy of t; therefore it is a legal use of the instanceof operator. (4) is also correct because Component is part of the hierarchy of t, because Ticker extends Component. (1) is incorrect because the syntax is wrong. A variable (or null) always appears before the instanceof operator, and a type appears after it. (3) is incorrect because the statement is used as a method (t.instanceof(Ticker);), which is illegal.

Multiple choice
  1. 1 only

  2. 2 only

  3. 3 and 5

  4. 1 and 4

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

(3) are (5) are correct because interfaces and abstract classes do not need to fully implement the interfaces they extend or implement (respectively). (1) is incorrect because a class cannot extend an interface. (2) is incorrect because an interface cannot implement anything. (4) is incorrect because the method being implemented is from the wrong interface.

Multiple choice
  1. Foo.Bar b = new Foo.Bar();

  2. Foo.Bar b = f.new Bar();

  3. Bar b = new f.Bar();

  4. Bar b = f.new Bar();

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

Option B is correct because the syntax is correct-using both names (the enclosing class and the inner class) in the reference declaration, then using a reference to the enclosing class to invoke new on the inner class. Option A, C and D all use incorrect syntax. A is incorrect because it doesn't use a reference to the enclosing class, and also because it includes both names in the new. C is incorrect because it doesn't use the enclosing class name in the reference variable declaration, and because the new syntax is wrong. D is incorrect because it doesn't use the enclosing class name in the reference variable declaration.

Multiple choice
  1. final

  2. static

  3. private

  4. protected

  5. volatile

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

The private access modifier limits access to members of the same class. Option A, B, D, and E are wrong because protected are the wrong access modifiers, and final, static, and volatile are modifiers but not access modifiers.