Which of the following best demonstrates the Liskov Substitution Principle in OOP?
AChild class can override parent methods with different return types
BChild class objects should be usable wherever parent class objects are expected
CParent class should not know about child class
DChild class should inherit all methods from parent
Correct Answer:
B. Child class objects should be usable wherever parent class objects are expected
EXPLANATION
The Liskov Substitution Principle states that objects of a superclass should be replaceable with objects of its subclasses without breaking the application. Option B correctly represents this principle.
In a legacy system, you find code with very deep inheritance hierarchy (5+ levels). What design problem does this indicate?
AGood object-oriented design
BViolation of the Liskov Substitution Principle
CPossible violation of 'favor composition over inheritance' principle
DThe code is efficient and should be kept as is
Correct Answer:
C. Possible violation of 'favor composition over inheritance' principle
EXPLANATION
Deep inheritance hierarchies are considered a code smell. They suggest that composition might have been a better approach, leading to tighter coupling and reduced flexibility.
What is the output of the following code?
interface I1 { default void show() { System.out.println("I1"); } }
interface I2 { default void show() { System.out.println("I2"); } }
class C implements I1, I2 { public void show() { I1.super.show(); } public static void main(String[] args) { new C().show(); } }
AI1
BI2
CCompile-time error due to ambiguity
DI1 I2
Correct Answer:
A. I1
EXPLANATION
When a class implements multiple interfaces with the same default method, it must explicitly override the method. Using I1.super.show() calls I1's default implementation.
A real-world scenario: You're designing a banking system. Should you use an abstract class or interface for 'Account'?
AUse interface because it's more flexible
BUse abstract class because Account has state (balance, accountNumber) and shared behavior (deposit, withdraw)
CUse both abstract class and interface together
DNeither, use a concrete class
Correct Answer:
B. Use abstract class because Account has state (balance, accountNumber) and shared behavior (deposit, withdraw)
EXPLANATION
Abstract classes are suitable when you have shared state and constructors needed. An Account has properties like balance and account number, making abstract class the better choice.
What does the following code output?
class A { static void display() { System.out.println("A"); } }
class B extends A { static void display() { System.out.println("B"); } }
public class Test { public static void main(String[] args) { A ref = new B(); ref.display(); } }
AA
BB
CCompile-time error
DRuntime error
Correct Answer:
A. A
EXPLANATION
Static methods are resolved at compile-time based on the reference type, not the object type. Hence, A.display() is called, not B.display(). This is method hiding, not overriding.
What is the main difference between method overriding and method overloading?
AOverriding is for same class, overloading is for different classes
BOverriding changes method signature, overloading keeps it same
COverriding is runtime polymorphism, overloading is compile-time polymorphism
DOverriding requires final keyword, overloading doesn't
Correct Answer:
C. Overriding is runtime polymorphism, overloading is compile-time polymorphism
EXPLANATION
Method overloading is compile-time (static) polymorphism with same method name but different parameters in the same class. Method overriding is runtime (dynamic) polymorphism where a child class provides a specific implementation of a parent method.
What is the output of this code?
class A { int x = 10; }
class B extends A { int x = 20; }
public class Test { public static void main(String[] args) { A a = new B(); System.out.println(a.x); } }
A10
B20
CCompilation Error
DRuntime Error
Correct Answer:
A. 10
EXPLANATION
Variable shadowing occurs here. The reference type is A, so a.x accesses A's x field which is 10. Method overriding works with methods, not instance variables.
A company's codebase has a scenario where multiple unrelated classes need to implement a contract with specific methods. Which design choice is best?
ACreate an abstract parent class with abstract methods
BCreate an interface with abstract method declarations
CUse composition instead of inheritance
DCreate static utility methods in a common class
Correct Answer:
B. Create an interface with abstract method declarations
EXPLANATION
When unrelated classes need to implement a common contract, an interface is the best choice. Interfaces are specifically designed for this purpose, allowing multiple implementation without forcing an inheritance hierarchy.