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?
Answer: B
Using an interface with multiple implementations follows the Interface Segregation Principle and Strategy Pattern, making the system extensible and maintainable.
Q.182Hard
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() { }
}
Answer: B
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.
Q.183Hard
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?
Answer: B
An abstract class with abstract methods enforces that subclasses must implement required behaviors while preventing direct instantiation of the base class.
Q.184Hard
What is the correct order of execution when you create an object in a multi-level inheritance hierarchy?
Answer: B
Constructor execution follows the inheritance chain from top to bottom. super() is implicitly called, executing parent constructors first.
Q.185Hard
Which feature of Java ensures that a child class can have a method with a wider return type than the parent class?
Answer: B
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).
Advertisement
Q.186Hard
What is the key difference between composition and inheritance in object design?
Answer: B
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.
Q.187Easy
What is the default access modifier for a class member in Java if no modifier is specified?
Answer: D
When no access modifier is specified, the member has package-private (default) access, visible only within the same package.
Q.188Easy
Which keyword is used to prevent a class from being subclassed in Java?
Answer: B
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.
Q.189Medium
In Java, if a parent class has a method void display() and a child class overrides it as void display(int x), what is this called?
Answer: B
This is method overloading, not overriding, because the method signature is different (different parameters). Overriding requires the same signature.
Q.190Medium
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?
Answer: D
The interface provides abstraction by hiding implementation details. Multiple classes implementing the same interface method demonstrates polymorphism (many forms).
Q.191Medium
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);
}
}
Answer: A
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.
Q.192Medium
Which of the following statements about 'this' keyword in Java is INCORRECT?
Answer: C
'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.
Q.193Medium
In Java, a class can implement multiple interfaces but can extend only one class. This design choice primarily supports which principle?
Answer: C
Multiple inheritance of implementation can cause the diamond problem. Java avoids this by allowing single class inheritance but multiple interface implementation.
Q.194Hard
You need to create a class that provides common functionality for all database operations (insert, update, delete). What is the best approach?
Answer: D
An abstract class allows you to define both abstract methods (to be overridden) and concrete methods (shared implementation), which is ideal for this scenario.
Q.195Hard
In Java 16+, which sealed class feature allows you to restrict which classes can extend a class?
Answer: B
Java 17 introduced 'sealed' classes with the 'permits' clause to explicitly specify which classes can extend a sealed class, providing better control over inheritance.
Q.196Hard
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();
Answer: C
This demonstrates dynamic (runtime) polymorphism. The actual object type (ElectricCar) determines which method is called, not the reference type (Vehicle).
Q.197Hard
Which of the following correctly implements the Factory Design Pattern principle in OOP?
Answer: B
The Factory Pattern uses a separate class (factory) with methods to create and return objects. This decouples object creation from usage.
Q.198Hard
In a banking system, Account is a parent class with method withdraw(). SavingsAccount and CurrentAccount both override it. A programmer writes: List<Account> accounts = new ArrayList<>(); accounts.add(new SavingsAccount()); accounts.add(new CurrentAccount()); for(Account a : accounts) a.withdraw(1000); Which concept is primarily demonstrated here?
Answer: C
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.
Q.199Medium
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?
Answer: C
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.
Q.200Easy
Which interface does HashMap implement in Java Collections Framework?
Answer: A
HashMap implements the Map interface which stores key-value pairs. It does not implement Collection, List, or Set interfaces directly.