Practice
RoomManager 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
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 RoomManager
RoomManager manages shared state like room availability; having multiple instances risks inconsistent state. Singleton ensures centralized control.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized static getInstance() -> Singleton [check]
Seat entity be modeled as an abstract class or an interface, and why?Solution
Step 1: Analyze seat characteristics
Seats have shared state (seat number, class) and common behavior (availability, booking), which suggests code reuse.Step 2: Abstract class vs interface
Abstract class allows shared state and default behavior implementation, while interface cannot hold state and only defines method signatures.Final Answer:
Option A -> Option AQuick Check:
Shared state + behavior -> abstract class [check]
handleRequest() is not synchronized, which exact sequence of events can cause a request to be handled twice by two handlers?Solution
Step 1: Identify race condition
Without synchronization, two threads can both see canHandle() as true before either marks the request handled.Step 2: Trace thread interleaving
Thread 1 checks canHandle() -> true, context switch before marking handled; Thread 2 also checks canHandle() -> true and proceeds; both handle the same request.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap causes double handling [check]
loadPlugin() method checks if a plugin is already loaded before loading it. Without synchronizing this method, what exact thread interleaving causes the same plugin to be loaded twice?Solution
Step 1: Trace the check-then-act sequence without synchronization
Thread 1 calls isLoaded() and sees false, then context switches before loading.Step 2: Thread 2 also calls isLoaded() and sees false, both threads proceed to load plugin concurrently, causing duplicate loading.
Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act allows double loading due to race [check]
tryConsume() method in TokenBucketRateLimiter that checks if tokens are available and then decrements the count without synchronization. Which exact thread interleaving causes double token consumption?Solution
Step 1: Identify check-then-act race condition
Without synchronization, both threads can read tokens > 0 before either decrements.Step 2: Trace exact interleaving causing double consumption
Thread 1 checks tokens > 0 -> true, context switch; Thread 2 checks tokens > 0 -> true; both proceed to decrement, consuming same token twice.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act allows double token consumption [check]
