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]
Board class has a placePiece() method that first checks if a position is free and then places the piece. Without synchronizing placePiece(), what exact sequence of thread interleaving causes two threads to place different pieces at the same position (double-booking)?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 checks position free and sees true, but before placing, context switches.Step 2: Thread 2 also checks position free -> true and proceeds to place piece concurrently, causing double-booking.
Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act allows both threads to place pieces at same position [check]
addTrack() is not synchronized. Two threads concurrently check if the queue has space and then add tracks. Which exact sequence causes a race condition leading to queue overflow?Solution
Step 1: Identify the check-then-act pattern in addTrack()
Both threads check queue size before adding, but without synchronization, the state can change between check and add.Step 2: Trace the race condition sequence
Thread 1 checks size -> space available, context switch before adding. Thread 2 checks size -> space available, both add tracks, causing overflow.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> double add -> overflow [check]
selectItem() is not synchronized, which exact thread interleaving causes dispensing the same item twice?Solution
Step 1: Identify the check-then-act race
Without synchronization, both threads can read item availability as true before either decrements inventory.Step 2: Trace the interleaving
Thread 1 checks availability -> true, context switch; Thread 2 checks availability -> true; both proceed to dispense, causing double dispense.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> double dispense [check]
parkVehicle() is not synchronized, which exact sequence of Thread 1 and Thread 2 causes double-booking of the same spot?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available, then context switches before marking it occupied.Step 2: Trace Thread 2's actions
Thread 2 also reads spot as available and proceeds to park, causing double-booking.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> both threads see available -> double-booking [check]
