Practice
1. In the Online Tic-Tac-Toe game design, the
Board class uses a private constructor and a static synchronized getInstance() method to provide access. Which design pattern is applied here and why is this the correct choice?easy
Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()ensures a single instance is created and accessed safely in concurrent contexts.Step 2: Why Singleton fits the Board class
Since the game requires exactly one board per session to maintain consistent state, Singleton enforces this constraint. Factory and Builder patterns do not restrict instance count, and Prototype is for cloning, not single-instance control.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
Hint: Private constructor + static getInstance() -> Singleton [check]
Common Mistakes:
2. To add a
WeekendFeeStrategy for weekend pricing in the current booking system design, which minimal change respects the Open-Closed Principle (OCP)?medium
Solution
Step 1: Identify OCP requirement
OCP requires adding new functionality without modifying existing tested code.Step 2: Evaluate options
Add a boolean flagisWeekendinBookingand handle fees there. adds weekend flag in Booking, which violates OCP by modifying Booking class. Create a new class implementingFeeStrategyinterface for weekend fees and inject it without changing existing classes. creates a new FeeStrategy class, respecting OCP by extension. Modify the existingFeeCalculatorclass to add weekend logic with if-else conditions. and D modify existing classes, violating OCP.Final Answer:
Option C -> Option CQuick Check:
New FeeStrategy class -> extend without modifying existing code [check]
Hint: New strategy class -> extend, don't modify [check]
Common Mistakes:
3. In a multi-threaded environment, if the
clone() method is not synchronized, which exact sequence of events can cause two threads to receive the same cloned object instance, leading to shared mutable state?medium
Solution
Step 1: Identify race condition in clone()
Without synchronization, clone() can copy partial or inconsistent state if interrupted mid-copy.Step 2: Trace exact interleaving causing shared mutable state
Thread 1 copies partial state, context switch to Thread 2 which copies same partial state, both return references to the same underlying mutable object.Final Answer:
Option B -> Option BQuick Check:
Unsynchronized clone() -> partial copy race -> shared mutable instance [check]
Hint: Unsync clone() -> partial copy race -> shared object [check]
Common Mistakes:
4. To add a
WeekendFeeStrategy for weekend parking fees without modifying existing classes, which SOLID principle is being applied and what is the minimal required change?medium
Solution
Step 1: Identify SOLID principle
Open-Closed Principle (OCP) requires extending behavior without modifying existing classes.Step 2: Apply minimal change
Create newWeekendFeeStrategyimplementingFeeStrategyand inject it; no existing class changes needed.Final Answer:
Option B -> Option BQuick Check:
OCP: new fee class added, existing classes unchanged [check]
Hint: OCP: extend with new class, no existing code changes [check]
Common Mistakes:
5. The system needs to support
ElectricCar vehicles and ChargingSpot parking spots without modifying existing classes. What is the minimum change to the current Java design to support this extensibility?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and new subclasses allows extension without changing existing class code, preserving OCP.Step 2: Why updating compatibility map is needed
SpotCompatibility map controls which vehicle types fit which spots; updating it supports new ElectricCar and ChargingSpot types without class changes.Final Answer:
Option C -> Option CQuick Check:
New enums + subclass + compatibility map update -> no existing class changes [check]
Hint: Extend enums + subclasses + compatibility map, no class edits [check]
Common Mistakes:
