Practice
OrderManager 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
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single shared instance, which matches the Singleton pattern.Step 2: Why Singleton fits OrderManager
OrderManager must coordinate all orders centrally to avoid conflicting states; multiple instances would cause inconsistent order tracking.Final Answer:
Option C -> Option CQuick Check:
Private constructor + synchronized static getInstance() -> Singleton [check]
Board 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 role of private constructor and static synchronized method
The private constructor prevents external instantiation, and the synchronizedgetInstance()ensures thread-safe single instance creation.Step 2: Match to design pattern intent
Singleton pattern ensures exactly oneBoardinstance exists, which is critical to maintain consistent game state.Final Answer:
Option A -> Option AQuick Check:
Singleton controls single instance creation with thread safety [check]
sendNotification() method checks if a channel is available before sending. Without synchronizing this method, what exact sequence of thread interleaving causes duplicate notifications to be sent?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, both threads can see the channel as available before either marks it busy.Step 2: Trace thread interleaving
Thread 1 checks availability -> true, context switch; Thread 2 checks availability -> true; both proceed to send, causing duplicate notifications.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act allows concurrent sends [check]
WeekendFeeStrategy for weekend parking fees without modifying existing classes, which SOLID principle is being applied and what is the minimum required change?Solution
Step 1: Identify SOLID principle for extensibility
Open/Closed Principle states software entities should be open for extension but closed for modification.Step 2: Apply OCP by adding new WeekendFeeStrategy implementing FeeStrategy interface without modifying ParkingLot.
Final Answer:
Option C -> Option CQuick Check:
New strategy class added, existing classes unchanged -> OCP respected [check]
Solution
Step 1: Identify extensibility requirements
Supporting ElectricCar and ChargingSpots requires new types without changing existing class logic to respect OCP.Step 2: Apply minimal change approach
Adding new enum values and subclass ElectricCar allows extension without modifying existing classes. Updating SpotCompatibility map centralizes compatibility logic.Step 3: Eliminate options violating OCP
Options B, C, and D modify existing classes or add conditional logic, violating OCP and risking class explosion or god classes.Final Answer:
Option C -> Option CQuick Check:
New enums + subclass + compatibility map update -> minimal, OCP-compliant [check]
