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.
Which of the following is true about interface default methods?
AThey must be implemented by all implementing classes
BThey provide a default implementation that can be overridden or inherited
CThey are the same as abstract methods
DOnly one interface can have default methods
Correct Answer:
B. They provide a default implementation that can be overridden or inherited
EXPLANATION
Default methods in interfaces (Java 8+) provide a default implementation. Implementing classes can use this default implementation or override it as needed.
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 relationship between an abstract class and an interface in Java 8+?
AAbstract classes and interfaces are identical
BInterfaces can have default and static methods, while abstract classes can have instance variables and constructors
COnly abstract classes can be extended
DInterfaces are always preferred over abstract classes
Correct Answer:
B. Interfaces can have default and static methods, while abstract classes can have instance variables and constructors
EXPLANATION
From Java 8, interfaces can have default and static methods, but abstract classes can have instance variables, constructors, and private methods which interfaces cannot have.
Consider a scenario where class B extends class A. If class A has a constructor with parameters, what must class B do?
AIgnore the parent constructor completely
BMust explicitly call parent constructor using super()
CParent constructor is automatically called without any action
DClass B becomes invalid and cannot be compiled
Correct Answer:
B. Must explicitly call parent constructor using super()
EXPLANATION
If a parent class has a parameterized constructor and no default constructor, the child class must explicitly call it using super() to initialize parent class members.