Practice
FareCalculator class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is this the correct use case?Solution
Step 1: Identify the pattern from constructor and method signatures
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single shared instance, matching Singleton pattern characteristics.Step 2: Why Singleton fits FareCalculator
FareCalculator needs consistent fare rules and shared state (e.g., pricing configs). Singleton avoids multiple inconsistent instances and ensures thread-safe access.Final Answer:
Option C -> Option CQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
WeekendFeeStrategy to calculate parking fees differently on weekends using the current adapter-based design. What is the minimum change required to comply with SOLID principles?Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires extending behavior without modifying existing code.Step 2: Evaluate options
Create a new WeekendFeeStrategy class implementing FeeStrategy and inject it where needed without modifying existing classes. respects OCP by adding a new class implementing FeeStrategy. Add a boolean flag to Vehicle to indicate weekend and modify ParkingLot to check it. modifies Vehicle and ParkingLot, violating OCP. Options A and D modify existing classes.Final Answer:
Option A -> Option AQuick Check:
New strategy class extends behavior without modifying existing code [check]
KitchenQueue class has a parkOrder() method that checks if a slot is available before assigning an order. Without synchronization on parkOrder(), which exact thread interleaving causes double-booking of the same slot?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads slot as available but is preempted before assignment; Thread 2 also reads available and proceeds.Step 2: Both threads assign the same slot leading to double-booking
Because the availability check and assignment are not atomic, both threads assign the same slot concurrently.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without sync -> double assignment [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]
WeekendFeeStrategy for job posting fees without modifying existing classes. According to SOLID principles, what is the minimum change required to support this?Solution
Step 1: Identify the SOLID principle involved
The Open-Closed Principle (OCP) requires that classes be open for extension but closed for modification.Step 2: Apply OCP to fee strategy
Creating a newWeekendFeeStrategyclass implementing the existingFeeStrategyinterface allows extension without modifying existing classes.Final Answer:
Option D -> Option DQuick Check:
New FeeStrategy class extends behavior without modifying existing code [check]
