Practice
AuctionManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is this the correct choice?Solution
Step 1: Identify Singleton intent
The private constructor and static synchronizedgetInstance()method ensure only one instance ofAuctionManagerexists, centralizing auction state management.Step 2: Eliminate other patterns
Factory creates multiple objects, Builder incrementally builds complex objects, Prototype clones instances--none enforce single global instance.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
WeekendFeeStrategy to calculate parking fees differently on weekends using the current adapter-based design. What is the minimum change required to comply with SOLID principles?Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires extending behavior without modifying existing code.Step 2: Evaluate options
Create a new WeekendFeeStrategy class implementing FeeStrategy and inject it where needed without modifying existing classes. respects OCP by adding a new class implementing FeeStrategy. Add a boolean flag to Vehicle to indicate weekend and modify ParkingLot to check it. modifies Vehicle and ParkingLot, violating OCP. Options A and D modify existing classes.Final Answer:
Option A -> Option AQuick Check:
New strategy class extends behavior without modifying existing code [check]
addProductToCart() method is not synchronized, which exact sequence of events can cause the cart to contain duplicate entries for the same product?Solution
Step 1: Identify check-then-act race condition
Both threads check if product exists and see false before either adds it, due to lack of synchronization.Step 2: Both threads add product concurrently causing duplicates
Because the add operation is not atomic, both add the product, resulting in duplicates.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap without synchronization -> duplicate entries [check]
OrderBook class, the placeOrder() method checks if the order slot is available and then books it. Without synchronization on placeOrder(), which exact sequence of thread interleaving causes double-booking of the same slot?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads slot as free but is preempted before booking; Thread 2 also reads free and proceeds to book.Step 2: Why this causes double-booking
Both threads believe the slot is free and book it concurrently, causing inconsistent state and double-booking.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without lock -> double-booking [check]
Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot requires new types without modifying existing classes to respect OCP.Step 2: Evaluate options
Add ELECTRIC_CAR to VehicleType enum and CHARGING to SpotType enum, create ElectricCar subclass, update SpotCompatibility map adds new enum values and subclasses without changing existing classes, enabling extension. Options B, C, and D modify existing classes or interfaces, violating OCP.Final Answer:
Option C -> Option CQuick Check:
New enums and subclass -> no existing class modified [check]
