Practice
getInstance() method that returns the single instance. Which design pattern is applied here and why is this the CORRECT use case?Solution
Step 1: Identify the pattern from constructor and method signature
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method controls instance creation, which matches Singleton pattern characteristics.Step 2: Explain why Singleton fits this use case
Singleton ensures a single instance with thread-safe access via synchronization, suitable for shared resources like configuration or logging.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized static getInstance() -> Singleton pattern [check]
assignLocker() method is not synchronized, which exact sequence of thread interleaving causes two packages to be assigned the same locker?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads locker as available, then context switches before marking it occupied.Step 2: Thread 2 also reads locker as available and proceeds to assign it, causing double-booking.
Both threads assign the same locker before either updates the status.Final Answer:
Option C -> Option CQuick Check:
Check-then-act gap -> double assignment [check]
WeekendFeeStrategy without modifying existing classes, which SOLID principle is being applied and what is the minimal required change?Solution
Step 1: Identify SOLID principle
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Apply OCP via new strategy class
Adding WeekendFeeStrategy as a new implementation of FeeStrategy extends behavior without modifying existing classes.Final Answer:
Option D -> Option DQuick Check:
New strategy class extends fees without changing existing code [check]
FriendRequestManager class has a sendRequest() method that checks if a friend request exists before creating one. Without synchronization, which exact thread interleaving causes duplicate friend requests?Solution
Step 1: Identify the check-then-act race condition
Both threads check for existing request before creation; without synchronization, both see none.Step 2: Trace thread interleaving
Thread 1 checks and pauses before creating; Thread 2 checks and proceeds; both create duplicate requests.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act -> duplicate friend requests [check]
WeekendFeeStrategy for weekend parking fees without modifying existing classes, which SOLID principle is being applied and what is the minimum required change?Solution
Step 1: Identify SOLID principle for extensibility
Open/Closed Principle states software entities should be open for extension but closed for modification.Step 2: Apply OCP by adding new WeekendFeeStrategy implementing FeeStrategy interface without modifying ParkingLot.
Final Answer:
Option C -> Option CQuick Check:
New strategy class added, existing classes unchanged -> OCP respected [check]
