Practice
OrderManager 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 the pattern from constructor and method signature
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 this use case
OrderManager must coordinate all orders centrally to avoid conflicting states; Singleton guarantees a single shared instance.Final Answer:
Option C -> Option CQuick Check:
Private constructor + synchronized static getInstance() -> Singleton pattern [check]
Condition component be an abstract class or an interface, and why?Solution
Step 1: Analyze Condition's role
Conditions define evaluation logic without requiring shared state; they represent a contract for boolean checks.Step 2: Interface vs Abstract class
Interface suits stateless behavior contracts and allows multiple inheritance; abstract class implies shared state, which is not needed and limits flexibility.Final Answer:
Option A -> Option AQuick Check:
Condition defines behavior only -> interface [check]
ElevatorController class uses a private constructor and a static synchronized getInstance() method to provide a single global access point. Which design pattern is applied here and why is it the correct choice?Solution
Step 1: Identify Singleton usage
The private constructor and static synchronizedgetInstance()method restrict instantiation to one object, ensuring a single ElevatorController.Step 2: Why Singleton fits ElevatorController
ElevatorController coordinates multiple elevators and floor queues centrally, so multiple instances would cause inconsistent state and scheduling conflicts.Final Answer:
Option B -> Option BQuick Check:
Singleton ensures one global ElevatorController instance [check]
Hand class has a method addCard(Card card) that is not synchronized. Two threads concurrently add cards to the same hand. Which exact sequence causes a race condition leading to lost cards?Solution
Step 1: Identify the check-then-act gap
Without synchronization, both threads read the current size of the card list before either adds their card.Step 2: Trace interleaving causing overwrite
Thread 1 reads size N, context switches; Thread 2 reads size N; both add at index N, causing one card to overwrite the other.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized addCard -> check-then-act race -> lost card [check]
updateStock() without synchronization, which exact sequence causes a race condition leading to incorrect stock count?Solution
Step 1: Trace the check-then-act gap
Thread 1 reads stock=10 but is preempted before writing update. Thread 2 reads the same stock=10 and writes stock=15. Thread 1 resumes and overwrites with stock=15, losing Thread 2's update.Step 2: Why other options fail
Thread 1 reads stock=10, Thread 2 reads stock=10, both add 5, final stock=15 incorrectly assumes final stock=15 after both add 5 (should be 20). Thread 1 writes stock=15, Thread 2 reads stock=15, then writes stock=20 correctly shows correct sequence with no race. Thread 1 and Thread 2 both wait on a lock, preventing race conditions describes locking, which is absent here.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized read-update-write -> lost update race condition [check]
