Practice
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]
Transaction class has a private constructor and a static synchronized getInstance() method that returns the single instance. Which design pattern is applied here and why is this the correct use case?Solution
Step 1: Identify the pattern from constructor and method signatures
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single instance is created and accessed thread-safely.Step 2: Why Singleton fits this Wallet Transaction scenario
Singleton is appropriate when exactly one instance is needed to coordinate actions or maintain global state, such as a ledger manager or transaction coordinator. Factory Method and Builder patterns do not enforce single instance, and Prototype is for cloning, not restricting instances.Final Answer:
Option B -> Option BQuick Check:
Private constructor + synchronized static getInstance() -> Singleton pattern [check]
Board class uses a private constructor and a static synchronized getInstance() method to provide access. Which design pattern is applied here and why is this the correct choice?Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()ensures a single instance is created and accessed safely in concurrent contexts.Step 2: Why Singleton fits the Board class
Since the game requires exactly one board per session to maintain consistent state, Singleton enforces this constraint. Factory and Builder patterns do not restrict instance count, and Prototype is for cloning, not single-instance control.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
parkVehicle() is not synchronized, which exact thread interleaving causes double-booking of the same spot?Solution
Step 1: Identify race condition in parkVehicle()
Without synchronization, two threads can both check spot availability before either marks it occupied.Step 2: Trace exact interleaving
Thread 1 calls isAvailable() -> true, context switch before marking occupied; Thread 2 calls isAvailable() -> true; both proceed to park, causing double-booking.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without synchronization causes double-booking [check]
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and a subclass extends functionality without changing existing class code, respecting OCP.Step 2: Evaluate options
ModifyVehicleclass to include achargingRequiredflag and update all spot checks. modifies Vehicle, violating OCP. Create a newChargingSpotclass and modifyParkingLotto handle it specially. requires modifying ParkingLot, violating minimal change. Add a new interfaceChargeableand makeElectricCarimplement it, changing existing spot logic. adds interface but requires changing spot logic, violating minimal change.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + compatibility map update -> minimal change [check]
