What is the relationship between an abstract class and an interface in Java 8+?
Answer: B
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.
Q.162Hard
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.163Medium
Which of the following is true about interface default methods?
Answer: B
Default methods in interfaces (Java 8+) provide a default implementation. Implementing classes can use this default implementation or override it as needed.
Q.164Hard
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.165Hard
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.
Advertisement
Q.166Medium
Which of the following correctly demonstrates composition over inheritance?
Answer: B
Composition (HAS-A relationship) is often preferred over inheritance (IS-A relationship) because it's more flexible and avoids tight coupling.
Q.167Hard
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.168Hard
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.169Easy
What is the access modifier of members declared in an interface?
Answer: C
All members in an interface are implicitly public. Variables are public static final, and methods are public abstract (unless default or static).
Q.170Easy
What is the default access modifier for class members in Java if not explicitly specified?
Answer: D
If no access modifier is specified for a class member, it has package-private (default) access, meaning it's accessible only within the same package.
Q.171Easy
Which keyword is used to prevent a class from being subclassed?
Answer: B
The 'final' keyword prevents a class from being extended. For example: final class MyClass {} cannot be subclassed.
Q.172Easy
Can you instantiate an interface in Java?
Answer: B
Interfaces cannot be instantiated directly. You can only create objects of classes that implement interfaces.
Q.173Medium
Which of the following demonstrates proper method overloading in Java?
Answer: C
Method overloading requires different parameter types or number of parameters. Options A and D have different return types (not valid for overloading), and option B has the same signature with different parameter names (not overloading).
Q.174Medium
What will happen if you try to override a final method in a subclass?
Answer: B
A final method cannot be overridden. Attempting to override it results in a compilation error: 'cannot override final method'.
Q.175Medium
In Java, which of the following is true about abstract classes?
Answer: A
An abstract class can have zero or more abstract methods. It can also have concrete methods, constructors, and instance variables.
Q.176Medium
What is the correct way to call a parent class constructor from a child class?
Answer: B
The super() keyword is used to call the parent class constructor. It must be the first statement in the child class constructor.
Q.177Medium
Which statement about Java interfaces is INCORRECT according to 2024 specifications?
Answer: D
Interfaces cannot be instantiated directly. Since Java 8, interfaces can have static and default methods, and since Java 9, they can have private methods.
Q.178Medium
Consider a class hierarchy: Animal -> Dog -> Puppy. A Puppy reference can access which of the following?
Answer: C
Through inheritance, a Puppy reference can access all non-private methods from Puppy, Dog, and Animal classes through the inheritance chain.
Q.179Medium
What is the output of this code?
class Test {
static int x = 10;
public static void main(String[] args) {
System.out.println(x++);
}
}
Answer: A
The post-increment operator (x++) returns the value before incrementing. So it prints 10, and then x becomes 11.
Q.180Hard
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.