In Java, when you override a method from a parent class, which of the following is NOT a valid reason to use the @Override annotation?
ATo enable compile-time checking for correct method signature
BTo improve code readability and maintainability
CTo automatically invoke the parent class method implementation
DTo catch errors if the parent method is removed or signature changes
Correct Answer:
C. To automatically invoke the parent class method implementation
EXPLANATION
The @Override annotation helps verify that you are correctly overriding a parent method and improves code clarity, but it does NOT automatically invoke the parent class method. You must use super.methodName() for that. @Override is purely a compile-time marker for verification purposes.
In Java, a class can implement multiple interfaces but can extend only one class. This design choice primarily supports which principle?
ASingle Responsibility Principle
BLiskov Substitution Principle
CAvoiding diamond problem in single inheritance
DInterface Segregation Principle
Correct Answer:
C. Avoiding diamond problem in single inheritance
EXPLANATION
Multiple inheritance of implementation can cause the diamond problem. Java avoids this by allowing single class inheritance but multiple interface implementation.
Which of the following statements about 'this' keyword in Java is INCORRECT?
A'this' can be used to refer to the current object
B'this' can be used to invoke another constructor of the same class
C'this' can be used in static methods
D'this' can be used to return the current object
Correct Answer:
C. 'this' can be used in static methods
EXPLANATION
'this' cannot be used in static methods because static methods are class-level and don't have an instance context. 'this' always refers to an instance.
What will be printed when this code executes?
class A { int x = 5; }
class B extends A { int x = 10; }
public class Test {
public static void main(String[] args) {
A a = new B();
System.out.println(a.x);
}
}
A5
B10
CCompilation error
DRuntime exception
Correct Answer:
A. 5
EXPLANATION
Variable shadowing occurs here. 'a' is of type A, so a.x refers to A's variable x which is 5. Variables are not overridden, only methods are.
Consider a scenario where you have an interface Shape with a method calculateArea(). You implement this in classes Circle and Rectangle. Which OOP concept is being demonstrated?
AEncapsulation
BAbstraction
CPolymorphism
DBoth Abstraction and Polymorphism
Correct Answer:
D. Both Abstraction and Polymorphism
EXPLANATION
The interface provides abstraction by hiding implementation details. Multiple classes implementing the same interface method demonstrates polymorphism (many forms).
Which statement about Java interfaces is INCORRECT according to 2024 specifications?
AInterfaces can have static methods
BInterfaces can have default methods
CInterfaces can have private methods
DInterfaces can be instantiated directly
Correct Answer:
D. Interfaces can be instantiated directly
EXPLANATION
Interfaces cannot be instantiated directly. Since Java 8, interfaces can have static and default methods, and since Java 9, they can have private methods.