When a static method is called on an instance of a class, what happens?
Answer: B
Static methods belong to the class, not instances. When called on an instance, Java internally calls the method on the class. Static methods cannot access instance variables or use 'this' keyword.
Q.22Hard
A company's codebase has a scenario where multiple unrelated classes need to implement a contract with specific methods. Which design choice is best?
Answer: B
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.
Q.23Hard
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); } }
Answer: A
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.
Q.24Hard
In Java, can you create an object of an abstract class?
Answer: C
Abstract classes cannot be directly instantiated, but you can create objects using anonymous inner classes that implement the abstract methods.
Q.25Hard
What is the main difference between method overriding and method overloading?
Answer: C
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.
Advertisement
Q.26Hard
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(); } }
Answer: A
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.
Q.27Hard
A real-world scenario: You're designing a banking system. Should you use an abstract class or interface for 'Account'?
Answer: B
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.
Q.28Hard
What happens with exception handling in method overriding?
Answer: C
Liskov Substitution Principle: A child class method can throw the same exception or a narrower exception than the parent method, not a broader one.
Q.29Hard
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(); } }
Answer: A
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.
Q.30Hard
In a legacy system, you find code with very deep inheritance hierarchy (5+ levels). What design problem does this indicate?
Answer: C
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.
Q.31Hard
Which of the following best demonstrates the Liskov Substitution Principle in OOP?
Answer: B
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.
Q.32Hard
Design scenario: You need to create a payment system where Credit Card, Debit Card, and UPI are payment methods. What's the best OOP approach?
Answer: B
Using an interface with multiple implementations follows the Interface Segregation Principle and Strategy Pattern, making the system extensible and maintainable.
Q.33Hard
What will be the compilation result of this code?
interface A { void method(); }
interface B { void method(); }
class C implements A, B {
public void method() { }
}
Answer: B
Java allows implementing multiple interfaces even if they have the same method signature. The class provides a single implementation that satisfies both interfaces. This compiles successfully.
Q.34Hard
In a real-world e-commerce system, you want to prevent direct instantiation of a base Product class but allow creation of Laptop, Mobile, and Tablet. Which approach is best?
Answer: B
An abstract class with abstract methods enforces that subclasses must implement required behaviors while preventing direct instantiation of the base class.
Q.35Hard
What is the correct order of execution when you create an object in a multi-level inheritance hierarchy?
Answer: B
Constructor execution follows the inheritance chain from top to bottom. super() is implicitly called, executing parent constructors first.
Q.36Hard
Which feature of Java ensures that a child class can have a method with a wider return type than the parent class?
Answer: B
Covariant return types (Java 5+) allow a method to return a subtype of the parent class method's return type. For example, if parent returns Animal, child can return Dog (subclass of Animal).
Q.37Hard
What is the key difference between composition and inheritance in object design?
Answer: B
Inheritance models an 'is-a' relationship (Dog is-an Animal), while composition models a 'has-a' relationship (Car has-an Engine). Composition is often preferred for flexibility.
Q.38Hard
You need to create a class that provides common functionality for all database operations (insert, update, delete). What is the best approach?
Answer: D
An abstract class allows you to define both abstract methods (to be overridden) and concrete methods (shared implementation), which is ideal for this scenario.
Q.39Hard
In Java 16+, which sealed class feature allows you to restrict which classes can extend a class?
Answer: B
Java 17 introduced 'sealed' classes with the 'permits' clause to explicitly specify which classes can extend a sealed class, providing better control over inheritance.
Q.40Hard
A system has classes: Vehicle -> Car -> ElectricCar. If Vehicle.start() is overridden in Car and again in ElectricCar, what is the output of: Vehicle v = new ElectricCar(); v.start();
Answer: C
This demonstrates dynamic (runtime) polymorphism. The actual object type (ElectricCar) determines which method is called, not the reference type (Vehicle).