What will be the output?
interface A {
void show();
}
class B implements A {
public void show() {
System.out.println("B");
}
}
class C extends B {
public void show() {
System.out.println("C");
}
}
A obj = new C();
obj.show();
Answer: C
Polymorphism in action. obj is of type A (interface), but actual object is C. C's show() method is called, printing 'C'.
Q.2Hard
What will be the output?
class Test {
int x = 5;
{
x = 10;
}
Test() {
x = 15;
}
public static void main(String[] args) {
Test t = new Test();
System.out.println(t.x);
}
}
Answer: C
Instance initializer block executes after variable initialization but before constructor. Constructor executes last, setting x = 15.
Q.3Hard
What is the output?
interface I1 {
void method1();
}
interface I2 extends I1 {
void method2();
}
class C implements I2 {
public void method1() { System.out.println("M1"); }
public void method2() { System.out.println("M2"); }
}
public class Test {
public static void main(String[] args) {
I1 obj = new C();
obj.method2();
}
}
Answer: C
obj is of type I1 which doesn't have method2(). Though actual object C has method2(), reference type determines what methods are accessible.
Q.4Hard
In Java, if a child class constructor does not explicitly call the parent class constructor using 'super()', what happens?
Answer: B
Java automatically inserts a call to the parent class's no-arg constructor if 'super()' is not explicitly called. This ensures parent class initialization happens before child class initialization.
Q.5Hard
A developer needs to create a class that cannot be extended and whose instances are immutable. Which keywords should be used?
Answer: B
To prevent inheritance, use 'final' on the class. To make instances immutable, declare all fields as 'final' and ensure they are not modified after initialization. A classic example is the String class.
Advertisement
Q.6Hard
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.7Hard
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.8Hard
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.9Hard
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.10Hard
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.
Q.11Hard
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.12Hard
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.13Hard
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.14Hard
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.15Hard
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.16Hard
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.17Hard
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.18Hard
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.19Hard
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.20Hard
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.