Which keyword is used to prevent a class from being subclassed in Java?
Aabstract
Bfinal
Cstatic
Dsealed
Correct Answer:
B. final
EXPLANATION
The 'final' keyword prevents a class from being extended. 'sealed' (Java 17+) allows selective subclassing, but 'final' is the standard way to prevent all subclassing.
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.
Which feature of Java ensures that a child class can have a method with a wider return type than the parent class?
AMethod Overloading
BMethod Overriding with Covariant Return Types
CRuntime Polymorphism
DMethod Hiding
Correct Answer:
B. Method Overriding with Covariant Return Types
EXPLANATION
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).
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?
AMake Product class final
BMake Product class abstract with abstract methods for subclasses to implement
CMake Product class private
DUse an interface for Product
Correct Answer:
B. Make Product class abstract with abstract methods for subclasses to implement
EXPLANATION
An abstract class with abstract methods enforces that subclasses must implement required behaviors while preventing direct instantiation of the base class.
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() { }
}
ACompilation error: ambiguous method
BCompiles successfully
CRuntime error
DCompilation error: method signature mismatch
Correct Answer:
B. Compiles successfully
EXPLANATION
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.
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?
ACreate a concrete PaymentCard class with if-else for each type
BCreate an interface PaymentMethod with implementations for each type
CUse a single abstract method for all payment types
DUse static methods to handle all payment types
Correct Answer:
B. Create an interface PaymentMethod with implementations for each type
EXPLANATION
Using an interface with multiple implementations follows the Interface Segregation Principle and Strategy Pattern, making the system extensible and maintainable.
Which of the following best demonstrates the Liskov Substitution Principle in OOP?
AChild class can override parent methods with different return types
BChild class objects should be usable wherever parent class objects are expected
CParent class should not know about child class
DChild class should inherit all methods from parent
Correct Answer:
B. Child class objects should be usable wherever parent class objects are expected
EXPLANATION
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.