Practice
Cart 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
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method controls instance creation, which matches the Singleton pattern.Step 2: Reason why Singleton fits Cart
Ensuring only one Cart instance per user session avoids inconsistent states and duplicate carts, which is the intended use case here.Final Answer:
Option B -> Option BQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
Vehicle abstraction for a parking system, should Vehicle be an abstract class or an interface if it needs to hold state like licensePlate? Why?Solution
Step 1: Identify state holding needs
licensePlateis shared state, which interfaces cannot hold as fields.Step 2: Evaluate options
Abstract classes can hold state and provide default behavior; interfaces cannot hold instance fields, only constants.Final Answer:
Option D -> Option DQuick Check:
Abstract class holds shared state like licensePlate [check]
parkVehicle() method checks if a parking spot is available and then marks it occupied. Without synchronization, which exact thread interleaving causes double-booking of the same spot?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as free (isAvailable() -> true) but is preempted before marking it occupied.Step 2: Thread 2 also reads spot as free and proceeds to mark it occupied, causing double-booking.
This is the classic check-then-act gap causing race conditions.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
put() method is not synchronized, which exact sequence of operations between Thread 1 and Thread 2 can cause the cache to contain duplicate keys or corrupt the doubly linked list?Solution
Step 1: Identify the race condition in put()
Without synchronization, two threads can both check for key existence and find it missing before either inserts, leading to duplicate nodes for the same key.Step 2: Trace the exact interleaving
Thread 1: check key -> false; context switch; Thread 2: check key -> false; both insert new nodes, corrupting the cache state.Final Answer:
Option D -> Option DQuick Check:
Concurrent check-then-act without lock -> duplicate keys inserted [check]
WeekendFeeStrategy to the parking lot pricing system. Which minimum change respects SOLID principles?Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires adding new functionality without modifying existing code.Step 2: Evaluate options
Modify the existingFeeCalculatorclass to add weekend logic inside. violates OCP by modifying existing class. Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it without changing existing classes. respects OCP by adding new strategy class. Add a boolean flagisWeekendtoVehicleand adjust pricing accordingly. incorrectly mixes state into Vehicle, violating SRP. Create a subclassWeekendFeeCalculatorextendingFeeCalculatorand override methods. adds subclass but may cause class explosion and tight coupling.Final Answer:
Option A -> Option AQuick Check:
New strategy class + no existing class change -> OCP respected [check]
