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 technology programming languages
  1. TestA

  2. TestB

  3. Compilation fails.

  4. An exception is thrown at runtime.

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

Despite casting new TestB() to TestA type, Java uses virtual method dispatch based on the actual object type. TestB overrides start(), so calling start() invokes TestB's implementation, printing 'TestB'.

Multiple choice technology programming languages
  1. Compilation will succeed for all classes and interfaces.

  2. Compilation of class C will fail because of an error in line 2.

  3. Compilation of class C will fail because of an error in line 6.

  4. Compilation of class AImpl will fail because of an error in line 2.

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

Class C's doit() method is valid because AImpl is a subclass of A, which is a valid covariant return type (narrowing the return type). However, execute() fails because you cannot widen a return type from String to Object when overriding - overriding methods must return the same type or a subtype, not a supertype. Option C correctly identifies line 6 as the compilation error.

Multiple choice technology programming languages
  1. Point p = Line.getPoint();

  2. Line.Point p = Line.getPoint();

  3. Point p = (new Line()).getPoint();

  4. Line.Point p = (new Line()).getPoint();

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

getPoint() is an instance method of class Line, so you need a Line instance to call it. Options A and B try to call it statically on the class itself, which is invalid. Option C creates the instance correctly but fails to specify the full type Line.Point for the inner class. Option D correctly creates a Line instance and properly declares the variable with the inner class type Line.Point.

Multiple choice technology programming languages
  1. Point p = new Point();

  2. Line.Point p = new Line.Point();

  3. The Point class cannot be instatiated at line 15.

  4. Line 1 = new Line() ; 1.Point p = new 1.Point();

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

Point is a static nested class defined inside the Line class. To instantiate it from outside the Line class, you must refer to it using the outer class name as 'Line.Point p = new Line.Point();'.

Multiple choice technology programming languages
  1. p0 = p1;

  2. p1 =p2;

  3. p2 = p4;

  4. p2 = (ClassC)p1;

  5. p1 = (ClassB)p3;

  6. p2 = (ClassC)p4;

Reveal answer Fill a bubble to check yourself
A,E,F Correct answer
Explanation

In Java, a subclass reference can be implicitly assigned to a superclass variable (p0 = p1). Downcasting requires an explicit cast, which is valid if the actual object matches the cast type (p1 = (ClassB)p3 and p2 = (ClassC)p4).

Multiple choice technology programming languages
  1. x.a2();

  2. z.a2();

  3. z.c1();

  4. z.a1();

  5. y.c1();

  6. x.a1();

Reveal answer Fill a bubble to check yourself
A,B,D,F Correct answer
Explanation

Polymorphic method calls work when the reference type has the method and the actual object's class overrides it. x.a2() and z.a2() are polymorphic - A declares a2(), B overrides it, and x/z reference B/C objects. x.a1() and z.a1() are polymorphic - A declares abstract a1(), B/C implement it. z.c1() and y.c1() are NOT polymorphic - reference type C has c1(), but reference z is type A (which doesn't have c1()), and y.c1() is just a normal call on C reference, not polymorphic.

Multiple choice technology programming languages
  1. insert a call to this() in the Car constructor

  2. insert a call to this() in the MeGo constructor

  3. insert a call to super() in the MeGo constructor

  4. insert a call to super(vin) in the MeGo constructor

  5. change the wheelCount variable in Car to protected

  6. change line 3 in the MeGo class to super.wheelCount = 3;

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

MeGo's constructor tries to access this.wheelCount, but wheelCount is private in Car. Even in a subclass, private members are not accessible - they must be protected or public. Option E correctly identifies this. Also, MeGo's constructor must call super(vin) to invoke Car's constructor since Car has no no-arg constructor - Java doesn't automatically insert super() when the parent lacks a no-arg constructor. Option D correctly identifies this.

Multiple choice technology programming languages
  1. public int blipvert(int x) { return 0; }

  2. protected long blipvert(int x, int y) { return 0; }

  3. private int blipvert(long x) { return 0; }

  4. protected long blipvert(int x) { return 0; }

  5. protected int blipvert(long x) { return 0; }

  6. protected long blipvert(long x) { return 0; }

Reveal answer Fill a bubble to check yourself
A,B,C,E,F Correct answer
Explanation

When overriding: A is valid (can widen access from protected to public, same signature). B compiles (different method - overloaded with two parameters). C compiles (different method - overloaded with long parameter). D fails - same signature but changing return type from int to long is not allowed. E compiles (overloaded with long parameter). F compiles (overloaded with long parameter). So A, B, C, E, F all compile.

Multiple choice technology programming languages
  1. Has-a relationships should never be encapsulated.

  2. Has-a relationships should be implemented using inheritance.

  3. Has-a relationships can be implemented using instance variables.

  4. Is-a relationships can be implemented using the extends keyword.

  5. Is-a relationships can be implemented using the implements keyword.

  6. An array or a collection can be used to implement a one-to-many has-a relationship.

Reveal answer Fill a bubble to check yourself
C,D,E,F Correct answer
Explanation

Has-a relationships (composition/aggregation) represent 'has-a' connections between classes and should use instance variables to hold references - option C is correct. Arrays/collections implement one-to-many has-a relationships - option F is correct. Has-a should NOT use inheritance (that's for is-a) - options A and B are wrong. Is-a relationships represent inheritance and use 'extends' for classes - option D is correct, or 'implements' for interfaces - option E is correct. So C, D, E, F are true.

Multiple choice technology programming languages
  1. public int hashCode()

  2. public boolean equals(Key k)

  3. public int compareTo(Object o)

  4. public boolean equals(Object o)

  5. public boolean compareTo(Key k)

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

For a class to work correctly as a HashMap key, it must override hashCode() and equals(Object). HashMap uses hashCode() to bucket entries and equals() to compare keys. The equals() method signature must be 'public boolean equals(Object o)' - overriding Object.equals() requires the Object parameter type, not Key. Option B's 'equals(Key k)' would be overloading, not overriding. Options A and D are correct - hashCode() and equals(Object) must both be overridden consistently.

Multiple choice technology programming languages
  1. True

  2. False

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

Using generic types (List) is strongly preferable over raw types (List). Raw types disable type checking, allowing runtime ClassCastException errors that generics would catch at compile-time. The 'verbiage' of generic types provides type safety - it's not needless. Joshua Bloch's Effective Java explicitly advises against raw types.

Multiple choice technology programming languages
  1. Poor Unicode support.

  2. java.lang.Cloneable does not contain clone() method.

  3. The byte type is signed.

  4. The creators of the Java programming language modeled the syntax after C and C++.

  5. java.io.InputStream is an abstract class and not an interface.

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

Joshua Bloch (author of Effective Java) has famously noted that the byte type being signed (range -128 to 127) is the strangest aspect of Java. This is because most hardware and languages use unsigned bytes (0-255), forcing awkward conversions when working with binary data. Other options are not quotes he's identified as 'strangest'.

Multiple choice technology programming languages
  1. process(bytes);

  2. BitUtils.process(bytes);

  3. app.BitUtils.process(bytes);

  4. util.BitUtils.process(bytes);

  5. import util.BitUtils. *; process(bytes);

  6. SomeApp cannot use the process method in BitUtils.

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

The process() method in BitUtils is declared as private static. Private methods are not accessible outside their own class, even with fully qualified names or imports. SomeApp cannot access this method regardless of how it's called.

Multiple choice technology programming languages
  1. Error: Non static method intialize cannot accessed from static context main

  2. Error: Method initialize is already defined

  3. i=0 after i=369 j=318

  4. Error: incompatible types

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

Line int i=initialize(369) tries to call initialize(int) which has void return type, but int i expects an int value. This is a type mismatch - void methods cannot return values to initialize variables.

Multiple choice technology programming languages
  1. process(bytes);

  2. BitUtils.process(bytes);

  3. util.BitUtils.process(bytes);

  4. SomeApp cannot use methods in BitUtils.

  5. import util.BitUtils.*; process(bytes);

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

Since SomeApp does not import the util package or the BitUtils class, it must use the fully qualified class name util.BitUtils.process(bytes); to call the static method.