Practice
ReservationManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is this the correct use case?Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single instance is created and accessed globally.Step 2: Why Singleton fits ReservationManager
ReservationManager must coordinate all reservations centrally to avoid conflicts; multiple instances would cause inconsistent state.Final Answer:
Option D -> Option DQuick Check:
Private constructor + static synchronized getInstance() -> Singleton pattern [check]
SeatManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is it the correct choice?Solution
Step 1: Identify the pattern from constructor and method signatures
A private constructor prevents external instantiation, and a static synchronizedgetInstance()method ensures controlled single instance creation.Step 2: Reason why Singleton fits seat management
SeatManager must coordinate seat availability globally to avoid conflicting bookings, so a single shared instance is necessary.Final Answer:
Option D -> Option DQuick Check:
Private constructor + static synchronized getInstance() -> Singleton pattern [check]
Bid entity be designed as an abstract class or an interface? Consider that bids have state (amount, bidder) and behavior (validate, notify). Which is the best choice and why?Solution
Step 1: Analyze state and behavior needs
Bids have shared state (amount, bidder) and common behavior (validation, notification), which benefit from code reuse and encapsulation.Step 2: Compare abstract class vs interface
Abstract classes can hold state and provide default method implementations; interfaces cannot hold state and only define method signatures.Final Answer:
Option C -> Option CQuick Check:
Stateful entity with shared behavior -> abstract class [check]
Vehicle be modeled as an abstract class or an interface if it has state like speed and behavior like accelerate()? Why?Solution
Step 1: Analyze state and behavior requirements
Vehicle has state (speed) and behavior (accelerate()), so it needs a construct that supports instance variables and method implementations.Step 2: Evaluate abstract class vs interface
Abstract classes can hold state and provide default behavior; interfaces cannot hold instance state (only constants) and default methods can't maintain state.Final Answer:
Option B -> Option BQuick Check:
State + behavior with shared code -> abstract class [check]
ElectricCar that requires ChargingSpot compatibility in the current parking system. What is the minimum change to the design to support this without modifying existing classes?Solution
Step 1: Identify extensibility requirements
ElectricCar and ChargingSpot are new types requiring compatibility mapping without changing existing classes.Step 2: Apply minimal invasive change
Adding new enum values and subclass plus updating compatibility map extends functionality without modifying existing class code, preserving OCP.Step 3: Avoid violating OCP
Options B, C, and D modify existing classes or create parallel hierarchies, increasing complexity and violating OCP.Final Answer:
Option B -> Option BQuick Check:
Extend enums and map, subclass ElectricCar -> minimal change [check]
