Practice
1. A class
CarBuilder constructs a Car object step-by-step using methods like setEngine(), setWheels(), and finally build(). The Car constructor is private. Which design pattern is applied here and why is it the correct choice?easy
Solution
Step 1: Identify the construction process
The class uses multiple setter methods to configure parts of the object before finalizing withbuild(), indicating stepwise construction.Step 2: Match pattern intent
Builder pattern separates construction from representation, ideal for complex objects with many optional parts. Factory Method uses inheritance, Prototype clones objects, Singleton restricts instance count.Final Answer:
Option A -> Option AQuick Check:
Stepwise object creation with private constructor -> Builder pattern [check]
Hint: Stepwise construction with private constructor -> Builder pattern [check]
Common Mistakes:
2. In implementing a chain of handlers in Java, should the Handler interface be an abstract class or an interface, and why?
easy
Solution
Step 1: Understand Handler role
Handlers define a contract to process or pass requests; they typically do not share mutable state.Step 2: Evaluate Java features
Interfaces allow multiple inheritance and default methods (since Java 8), promoting flexibility; abstract classes share state but restrict inheritance.Final Answer:
Option A -> Option AQuick Check:
Interface defines contract without forcing shared state [check]
Hint: Interface for contract, abstract class for shared state [check]
Common Mistakes:
3. In designing a notification channel abstraction, should
NotificationChannel be an abstract class or an interface? Consider that each channel has state like connection status and common methods like connect() and send(). Why?easy
Solution
Step 1: Analyze state and behavior requirements
NotificationChannel needs to hold connection status (state) and provide common method implementations.Step 2: Choose between interface and abstract class
Interfaces cannot hold instance state; abstract classes can hold state and provide default behavior, making abstract class the correct choice.Final Answer:
Option A -> Option AQuick Check:
Abstract class supports shared state and default methods [check]
Hint: Stateful base -> abstract class; stateless contract -> interface [check]
Common Mistakes:
4. In designing the Workflow Engine, should the
Condition component be an abstract class or an interface, and why?easy
Solution
Step 1: Analyze Condition's role
Conditions define evaluation logic without requiring shared state; they represent a contract for boolean checks.Step 2: Interface vs Abstract class
Interface suits stateless behavior contracts and allows multiple inheritance; abstract class implies shared state, which is not needed and limits flexibility.Final Answer:
Option A -> Option AQuick Check:
Condition defines behavior only -> interface [check]
Hint: Stateless behavior contracts -> interface, not abstract class [check]
Common Mistakes:
5. To support ElectricCar and ChargingSpot in the existing parking system using Chain of Responsibility, what is the minimum change that respects extensibility and avoids modifying existing classes?
hard
Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot should not require modifying existing classes to avoid breaking OCP.Step 2: Evaluate options
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility.COMPATIBILITY_MAP accordingly. modifies enums and compatibility map, changing existing code; Modify Vehicle and Spot classes to add boolean isElectric and isChargingSpot flags, then update existing handlers to check these flags. adds flags and updates handlers without changing existing class hierarchies; Create a new ElectricCarHandler in the chain that handles electric cars and charging spots, modifying the chain setup to include it. modifies chain setup, which is acceptable but may require modifying chain initialization; Add a new method in Vehicle interface for charging compatibility and implement it in ElectricCar, modifying existing handlers to call it. modifies Vehicle interface, breaking existing implementations.Step 3: Choose minimal invasive approach
Modify Vehicle and Spot classes to add boolean isElectric and isChargingSpot flags, then update existing handlers to check these flags. adds minimal changes with flags and updates handlers, avoiding class explosion or interface changes.Final Answer:
Option B -> Option BQuick Check:
Flags added, existing classes unchanged, handlers updated [check]
Hint: Add flags and update handlers, avoid modifying classes [check]
Common Mistakes:
