Practice
User class has a private constructor and a static synchronized getInstance() method to manage user sessions. 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 controls instance creation, which matches Singleton pattern characteristics.Step 2: Why Singleton fits user session management
Singleton ensures a single global instance manages user sessions, avoiding multiple conflicting session managers.Final Answer:
Option A -> Option AQuick Check:
Private constructor + static synchronized getInstance() -> Singleton [check]
castVote() method is not synchronized, which exact sequence of thread interleaving causes a double vote count for the same user on a question?Solution
Step 1: Identify the check-then-act race condition
The critical issue is the check if the user has already voted. Without synchronization, both threads see no vote recorded and proceed.Step 2: Trace exact interleaving causing double vote
Thread 1 checks voted -> false, context switch; Thread 2 checks voted -> false; both cast votes, causing double count.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without sync -> double vote [check]
parkVehicle() method checks if a spot is available and then marks it occupied. Without synchronized, which exact thread interleaving causes double-booking?Solution
Step 1: Identify check-then-act race condition
Without synchronization, Thread 1 reads spot as available but is preempted before marking it occupied. Thread 2 also reads spot as available and proceeds to mark it occupied.Step 2: Understand resulting double-booking
Both threads believe the spot is free and park vehicles, causing double-booking due to the gap between check and update.Final Answer:
Option C -> Option CQuick Check:
Check-then-act gap without synchronization -> double-booking [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 but before marking it occupied, context switches to Thread 2 which also reads it as available.Step 2: Both threads proceed to park, causing double-booking
Because the state update is not atomic, both threads park vehicles in the same spot simultaneously.Final Answer:
Option C -> Option CQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
ElectricCar players who require ChargingSpot in the Tic-Tac-Toe game parking analogy, what is the minimum design change that respects extensibility without modifying existing classes?Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot should not require modifying existing classes to avoid violating OCP and creating class explosion.Step 2: Why Add
Adding new enum values and a subclass keeps existing classes unchanged. Updating the compatibility map centralizes compatibility logic without scattering changes.ELECTRIC_CARtoVehicleTypeenum,CHARGINGtoSpotType, createElectricCarsubclass, and updateSpotCompatibility.COMPATIBILITY_MAPaccordingly. is minimal and extensibleStep 3: Why other options violate extensibility
Options B, C, and D modify existing classes or add conditional logic, increasing coupling and violating OCP.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + compatibility map update -> minimal, extensible [check]
