Practice
getInstance() method returning a single instance. Which OOP concept or pattern is applied here and why is this the correct use case?Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single instance is created and accessed safely across threads.Step 2: Eliminate other creational patterns
Factory Method allows multiple instances, Builder focuses on stepwise construction, and Prototype clones objects; none enforce single instance with thread safety.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized static method -> Singleton pattern [check]
makeMove() method, which exact sequence of thread interleaving causes two players to move simultaneously, violating turn order?Solution
Step 1: Identify the check-then-act race condition
Both threads read the turn state before either updates it, causing both to believe it's their turn.Step 2: Trace thread interleaving
Thread 1 checks turn -> true, context switch before move; Thread 2 checks turn -> true, both move simultaneously, violating turn order.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap allows double move without synchronization [check]
parkVehicle() method is not synchronized, which exact sequence of events between Thread 1 and Thread 2 causes double-booking of the same parking spot?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available and is preempted before marking it occupied.Step 2: Thread 2 also reads spot as available and proceeds to park, causing double-booking.
This is the classic check-then-act gap causing race condition.Final Answer:
Option A -> Option AQuick Check:
Check availability -> true by both threads before marking occupied -> double-booking [check]
transferFunds() method checks if the source account has sufficient balance before debiting and crediting the destination. Without synchronization, what exact sequence of Thread 1 and Thread 2 causes double spending (overdraft)?Solution
Step 1: Identify the check-then-act race condition
Both threads read the balance before either updates it, so both see sufficient funds and proceed.Step 2: Trace interleaving causing overdraft
Thread 1 checks balance -> sufficient, context switch before debit; Thread 2 checks balance -> sufficient, both debit, resulting in overdraft.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap -> both threads see same balance -> double debit [check]
ElectricCar vehicles needing ChargingSpot parking. What is the minimum change to the current design to support this without modifying existing classes?Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclassing allows extension without changing existing class code, respecting OCP.Step 2: Update compatibility externally
UpdatingSpotCompatibilitymap outside existing classes avoids modifying their code, enabling new vehicle-spot matching.Final Answer:
Option D -> Option DQuick Check:
Extend enums + subclass + external map update -> no existing class changes [check]
