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
  1. The equals method does NOT properly override the Object.equals method.

  2. Compilation fails because the private attribute p.name cannot be accessed in line 5.

  3. To work correctly with hash-based data structures, this class must also implement the hashCode method.

  4. When adding Person objects to a java.util.Set collection, the equals method in line 4 will prevent duplicates.

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

The equals method does NOT properly override Object.equals because it takes a Person parameter instead of Object. A proper override must have the exact signature 'boolean equals(Object obj)'. This is an overload, not an override. Option B is incorrect because private fields of the same object type can be accessed. Option C is true but not the primary issue.

Multiple choice technology
  1. Object o = Old.get0(new LinkedList());

  2. Object o = Old.get0(new LinkedList<?>());

  3. String s = Old.get0(new LinkedList<String>());

  4. Object o = Old.get0(new LinkedList<Object>());

  5. String s = (String)Old.get0(new LinkedList<String>());

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

Option A compiles because LinkedList is raw and get0() returns Object. Option D compiles as LinkedList matches Object return. Option E compiles with the explicit cast to String. Option B fails because LinkedList> is wildcard-typed and get0() expects raw List. Option C fails because String != Object return type without casting.

Multiple choice technology
  1. Foo.beta() is a valid invocation of beta().

  2. Foo.alpha() is a valid invocation of alpha().

  3. Method beta() can directly call method alpha().

  4. Method alpha() can directly call method beta().

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

Static methods like alpha() are called on the class name (Foo.alpha()), not instances. Instance methods like beta() can call static methods directly, but static methods cannot call instance methods without an object reference.

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

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

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

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

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

Point is a static nested class inside Line. To instantiate it outside Line, use the fully qualified name Line.Point and call new Line.Point(). Static nested classes don't require an instance of the outer class.

Multiple choice technology
  1. import com.sun.scjp.Geodetics;

  2. import static com.sun.scjp.Geodetics;

  3. import static com.sun.scjp.Geodetics.*;

  4. package com.sun.scjp;

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

DIAMETER is a public static final field. Option A imports the class to access it. Option C uses static import (import static) to import all static members, making DIAMETER accessible directly. Option B is invalid syntax for static import. Option D declares a package, not an import.

Multiple choice technology
  1. foreach( x ) System.out.println(z);

  2. for( int z : x ) System.out.println(z);

  3. while( x.hasNext() ) System.out.println( x.next() );

  4. for( int i=0; i< x.length; i++ ) System.out.println(x[i]);

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

The varargs parameter int... x is treated as an array. Option B uses enhanced for-loop to iterate over x. Option D uses traditional for-loop with x.length and array indexing. Option A is invalid syntax. Option C is wrong because arrays don't have hasNext()/next() methods.

Multiple choice technology
  1. The code will compile without changes.

  2. The code will compile if public Tree() { Plant(); } is added to the Tree class.

  3. The code will compile if public Plant() { Tree(); } is added to the Plant class.

  4. The code will compile if public Plant() { this("fern"); } is added to the Plant class

  5. The code will compile if public Plant() { Plant("fern"); } is added to the Plant class.

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

Plant has only a parameterized constructor. Tree's no-arg constructor implicitly calls super(), which fails because Plant has no no-arg constructor. Adding public Plant() { this("fern"); } provides the missing no-arg constructor.

Multiple choice technology
  1. Compilation fails.

  2. An exception is thrown at runtime

  3. Synchronizing the run() method would make the class thread-safe.

  4. The data in variable "x" are protected from concurrent access problems.

  5. Declaring the doThings() method as static would make the class thread-safe.

  6. Wrapping the statements within doThings() in a synchronized(new Object()) { } block would make the classthread-safe.

Reveal answer Fill a bubble to check yourself
F Correct answer
Multiple choice technology
  1. Alpha a = x;

  2. Foo f = (Delta)x;

  3. Foo f = (Alpha)x;

  4. Beta b = (Beta)(Alpha)x;

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

x is a Beta object. Casting it to Delta (a subclass) fails at runtime with ClassCastException because the actual object is not a Delta. You cannot cast an object to a subclass type it doesn't actually instantiate.

Multiple choice technology
  1. Exception

  2. A,B,Exception

  3. Compilation fails because of an error in line 20.

  4. Compilation fails because of an error in line 14.

  5. A NullPointerException is thrown at runtime

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

When overriding a method, you cannot add new checked exceptions to the throws clause. Class A's process() declares no checked exceptions, so B cannot add "throws IOException". This violates Java's override rules. Line 20's catch block is fine - unused catch blocks are legal.

Multiple choice technology
  1. Compilation fails

  2. An exception is thrown at runtime.

  3. An instance of Forest is serialized.

  4. An instance of Forest and an instance of Tree are both serialized

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

Forest implements Serializable, but its field 'tree' is of type Tree, which does NOT implement Serializable. During serialization, Java attempts to serialize all non-transient object fields. When it tries to serialize the Tree object, it throws a NotSerializableException at runtime because Tree is not serializable.

Multiple choice technology
  1. It can be any class

  2. No class has access to base

  3. The class must belong to the geometry package

  4. The class must be a subclass of the class Hypotenuse

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

InnerTriangle is a package-private class (no access modifier before 'class'), meaning only classes within the geometry package can access it. Since base is a public field inside InnerTriangle, any class that can access InnerTriangle can also access base. Therefore, only classes in the geometry package can reference the variable base.

Multiple choice technology
  1. Inheritance represents an is-a relationship

  2. Inheritance represents a has-a relationship

  3. Interfaces must be used when creating a has-a relationship

  4. Instance variables can be used when creating a has-a relationship.

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

Inheritance in Java represents an 'is-a' relationship - a subclass 'is a' type of its superclass. Has-a relationships represent composition and are typically implemented using instance variables (e.g., a Car 'has an' Engine). Interfaces are not required for has-a relationships - you use composition directly with instance variables. Options A and D are both correct statements.

Multiple choice technology
  1. Compilation fails because of an error in line 3

  2. Compilation fails because of an error in line 7

  3. Compilation fails because of an error in line 9

  4. If you define D e = new E(), then e.bMethod() invokes the version of bMethod() defined in Line 5.

  5. If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in Line 5.

  6. If you define D e = (D)(new E()), then e.bMethod() invokes the version of bMethod() defined in Line 9.

Reveal answer Fill a bubble to check yourself
F Correct answer
Multiple choice technology
  1. foofoofoofoofoo

  2. foobarfoobarbar

  3. foobarfoofoofoo

  4. foobarfoobarfoo

  5. barbarbarbarbar

  6. foofoofoobarbar

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

Static members are accessed based on the reference type, not the actual object type. Base.FOO prints 'foo' (accessing Base's static variable). Sub.FOO prints 'bar' (accessing Sub's static variable). b.FOO prints 'foo' because b is declared as Base type. s.FOO prints 'bar' because s is declared as Sub type. ((Base)s).FOO prints 'foo' because the cast changes the reference type to Base. The result is 'foobarfoobarfoo'. Static members are not polymorphic - they depend on compile-time type.