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 web technology
  1. "X extends Y" is correct if and only if X is a class and Y is an interface

  2. "X extends Y" is correct if and only if X is an interface and Y is a class

  3. "X extends Y" is correct if X and Y are either both classes or both interfaces

  4. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces

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

In Java, 'extends' is used for both class inheritance and interface extension, but the types must match. A class can extend another class, and an interface can extend another interface. Option C correctly states that X extends Y works when both are classes or both are interfaces. Options A and B are wrong because a class cannot extend an interface (that's 'implements'), and an interface cannot extend a class. Option D is wrong because mixing classes and interfaces with 'extends' is invalid.

Multiple choice technology web technology
  1. -124

  2. -134

  3. -424

  4. -434

  5. -444

  6. Compilation fails

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

aa (A[]) matches sifter(A[]...) via varargs, resolving to "1" if no other method matches, but varargs is lowest priority. ba (B[]) matches sifter(B[]...) and sifter(B[] b1). sifter(B[] b1) is a direct match, so it's chosen, yielding "3". sifter(7) (int) boxes to Integer and matches sifter(Object o), yielding "4". Total is s += "1" (for aa?), wait. sifter(aa): is there a better match? A[] matches Object o directly (non-varargs) or A[]... (varargs). Since A[] is an object, sifter(Object) (non-varargs) is chosen over sifter(A[]...) (varargs). So sifter(aa) prints "4". sifter(ba) matches sifter(B[] b1) (exact non-varargs match), printing "3". sifter(7) matches sifter(Object), printing "4". Output is -434.

Multiple choice technology programming languages
  1. True

  2. False

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

Method overloading in Java requires methods to have different parameter lists (different number, types, or order of parameters). Return type alone is insufficient to distinguish overloaded methods - the compiler uses the parameter signature to resolve which method to call.

Multiple choice technology programming languages
  1. java.lang.Exception

  2. java.lang.Error

  3. java.lang.Throwable

  4. none of the above

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

In Java, the Throwable class is the common root superclass of all exceptions and errors in the language. Only objects that are instances of Throwable or its subclasses can be thrown by the JVM or the throw statement. Exception and Error both inherit directly from Throwable.

Multiple choice technology web technology
  1. Change the Carnivore interface to interface Carnivore<E extends Plant> extends Hungry<E> {}

  2. Change the Herbivore interface to interface Herbivore<E extends Animal> extends Hungry<E> {}

  3. Change the Sheep class to class Sheep extends Animal implements Herbivore<Plant> { public void munch(Grass x) {} }

  4. Change the Sheep class to class Sheep extends Plant implements Carnivore<Wolf> { public void munch(Wolf x) {} }

  5. Change the Wolf class to class Wolf extends Animal implements Herbivore<Grass> { public void munch(Grass x) {} }

  6. No changes are necessary

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

The compilation fails because Sheep implements Herbivore, but Herbivore requires E extends Plant while Sheep extends Animal (not Plant). Changing Herbivore to accept E extends Animal fixes this since Sheep is an Animal. Carnivore already works correctly because it accepts E extends Animal, and Sheep qualifies.

Multiple choice technology web technology
  1. return new ArrayList<Inn>();

  2. return new ArrayList<Hotel>();

  3. return new ArrayList<Object>();

  4. return new ArrayList<Business>();

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

The method return type is ArrayList. Option B works because you can return an ArrayList directly - the types match exactly. Option A fails because Inn is a subtype of Hotel, and ArrayList is not assignable to ArrayList (generics are invariant). Option C fails because Object is not a Hotel. Option D fails because Business is a supertype of Hotel, and ArrayList is not assignable to ArrayList.

Multiple choice technology web technology
  1. "X extends Y" is correct if and only if X is a class and Y is an interface

  2. "X extends Y" is correct if and only if X is an interface and Y is a class

  3. "X extends Y" is correct if X and Y are either both classes or both interfaces

  4. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces

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

In Java, 'extends' is used for both class inheritance and interface extension. A class can extend another class, and an interface can extend another interface. Option C correctly states that 'X extends Y' works when both are classes or both are interfaces. Options A and B are incorrect because 'extends' works in both directions (class-from-class and interface-from-interface), not just one. Option D is incorrect because a class cannot extend an interface (it implements it), and an interface cannot extend a class.

Multiple choice technology web technology
  1. -124

  2. -134

  3. -424

  4. -434

  5. -444

  6. Compilation fails

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

This tests varargs and method overloading resolution. For sifter(aa), A[] matches varargs A[]... so output '1'. For sifter(ba), B[] can match either B[]... (more specific) or B[] varargs - Java chooses the varargs version B[]... giving '2'. For sifter(7), the int autoboxes to Integer, which doesn't match array types, so Object is chosen giving '4'. Result: -1-2-4 = '-124'. But wait - the correct answer is '-434'. Let me reconsider: sifter(aa) calls sifter(A[]...)? A[] can match A[]... (varargs), but it can also match Object since arrays are Objects. The most specific is A[]..., so '1'. sifter(ba): B[] matches B[]... more specifically than Object, but there's also B[](not varargs). Java prefers the non-varargs version when both apply, so '3'. sifter(7): matches Object, so '4'. Result: -1-3-4 = '-134'. Hmm, still not matching. Let me re-examine the varargs rules.

Multiple choice technology programming languages
  1. True

  2. False

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

In most statically-typed languages including Java, method overloading requires different parameter lists - return type alone is insufficient to distinguish methods. The compiler would be unable to determine which method to call when the return value is not used or assigned. Overloaded methods must have unique signatures based on parameter types and count.

Multiple choice technology programming languages
  1. java.lang.Exception

  2. java.lang.Error

  3. java.lang.Throwable

  4. none of the above

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

In Java's exception hierarchy, all exceptions and errors inherit from java.lang.Throwable, which is the root class. Exception and Error are direct subclasses of Throwable. Exception is for recoverable conditions, Error for serious system errors. Throwable itself is rarely used directly in application code.

Multiple choice technology web technology
  1. If the equals() method returns true, the hashCode() comparison == might return false

  2. If the equals() method returns false, the hashCode() comparison == might return true

  3. If the hashCode() comparison == returns true, the equals() method must return true

  4. If the hashCode() comparison == returns true, the equals() method might return true

  5. If the hashCode() comparison != returns true, the equals() method might return true

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

The equals/hashCode contract: equal objects MUST have equal hashes, but unequal objects CAN have equal hashes (hash collisions are allowed). Option B: If equals() returns false, they might still have the same hashCode (collisions allowed). Option D: If hashCode matches, equals() might return true (but must not always). Options A, C, E violate the contract.

Multiple choice technology web technology
  1. Replace line 13 with private Map<String, int> accountTotals = new HashMap<String, int>();

  2. Replace line 13 with private Map<String, Integer> accountTotals = new HashMap<String, Integer>();

  3. Replace line 13 with private Map<String<Integer>> accountTotals = new HashMap<String<Integer>>();

  4. Replace lines 17–20 with int total = accountTotals.get(accountName); if (total == null) total = 0; return total;

  5. Replace lines 17–20 with Integer total = accountTotals.get(accountName); if (total == null) total = 0; return total;

  6. Replace line 24 with accountTotals.put(accountName, amount);

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

Changing the map to Map enables generics. In lines 17–20, total must be declared as Integer to check for null safely before returning, which automatically unboxes to int. For line 24, autoboxing allows passing the primitive amount directly into the map.

Multiple choice technology programming languages
  1. compile time error at line1

  2. compile time error at line2

  3. Run time exception

  4. None Of the above

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

When overriding a method, you cannot reduce visibility. The parent class C1 has m1() with default (package-private) access. The child class C2 attempts to override it with private access, which is MORE restrictive than package-private. This violates the override rules, causing a compile-time error at line 2 where the overriding method is declared.