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.
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.