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 a banking system, Account is a parent class with method withdraw(). SavingsAccount and CurrentAccount both override it. A programmer writes: List accounts = new ArrayList(); accounts.add(new SavingsAccount()); accounts.add(new CurrentAccount()); for(Account a : accounts) a.withdraw(1000); Which concept is primarily demonstrated here?
AEncapsulation
BAbstraction only
CPolymorphism and Runtime Binding
DInheritance only
Correct Answer:
C. Polymorphism and Runtime Binding
EXPLANATION
The same withdraw() call behaves differently for SavingsAccount and CurrentAccount based on the actual object type at runtime. This is polymorphism with runtime (dynamic) binding.
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();
AVehicle's start() executes
BCar's start() executes
CElectricCar's start() executes
DCompilation error
Correct Answer:
C. ElectricCar's start() executes
EXPLANATION
This demonstrates dynamic (runtime) polymorphism. The actual object type (ElectricCar) determines which method is called, not the reference type (Vehicle).
In Java 16+, which sealed class feature allows you to restrict which classes can extend a class?
AUsing 'final' keyword on all methods
BUsing 'sealed' keyword with 'permits' clause
CUsing 'protected' access modifier on constructor
DUsing package-private access on the class
Correct Answer:
B. Using 'sealed' keyword with 'permits' clause
EXPLANATION
Java 17 introduced 'sealed' classes with the 'permits' clause to explicitly specify which classes can extend a sealed class, providing better control over inheritance.
You need to create a class that provides common functionality for all database operations (insert, update, delete). What is the best approach?
ACreate an interface DatabaseOperations
BCreate an abstract class with abstract methods
CCreate a concrete class with final methods
DCreate an abstract class with some concrete methods for common logic
Correct Answer:
D. Create an abstract class with some concrete methods for common logic
EXPLANATION
An abstract class allows you to define both abstract methods (to be overridden) and concrete methods (shared implementation), which is ideal for this scenario.
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).