Computer Knowledge

Object-Oriented Programming

2,239 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 security
  1. The type safety mechanism in the Java language prevents the execution of malicious code

  2. Two classes with the same fully qualified name but which are defined by different instances of a class loader are NOT of the same type

  3. All signed classes are implicitly trusted and granted full access

  4. The principal role of a TrustManager is to determine if presented authentication credentials should be trusted

  5. Option 1 AND Option 4

  6. Option 2 AND Option 4

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

Option 2 is true: classes with identical fully qualified names loaded by different class loaders are treated as different types due to namespace isolation. Option 4 is also true: TrustManager specifically validates authentication credentials like X.509 certificates. Options 1 and 3 are false: type safety doesn't prevent all malicious code, and signed classes aren't automatically granted full access.

Multiple choice technology programming languages
  1. A static method may override another static method.

  2. A static method cannot override a non-static method.

  3. A non-static method cannot override a static method.

  4. A non-static method may be overloaded by a static method.

  5. A synchronized method cannot be overridden.

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

Static methods belong to the class, not instances - they can't override non-static methods (different binding mechanism), and non-static methods can't override static methods. Overloading a non-static method with a static one works by signature but is poor practice (different binding). Static methods can be hidden by other static methods (shadowing), not truly overridden. The synchronized keyword affects thread safety, not override capability.

Multiple choice technology
  1. Differences

  2. Similarities

  3. Shared properties

  4. Relationships

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

Business objects define and manage the RELATIONSHIPS between Business Components (linking them through joins and definitions). They don't track differences, similarities, or shared properties - those are component-level concerns.

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.