Practice
1. In a system using the Prototype pattern, should the
Vehicle base type be an abstract class or an interface? Why?easy
Solution
Step 1: Consider cloning and shared state
Cloning often requires copying internal state; an abstract class can hold this state and provide a defaultclone()implementation.Step 2: Evaluate interface limitations
Interfaces define behavior but cannot hold state or provide default cloning logic (in Java before default methods). Thus, abstract class fits better.Final Answer:
Option A -> Option AQuick Check:
Abstract class holds state + cloning logic -> correct OOP design [check]
Hint: Clone needs shared state -> abstract class better than interface [check]
Common Mistakes:
2. In the Car Rental System, the
parkVehicle() method is not synchronized. Which exact sequence of events between Thread 1 and Thread 2 causes a double-booking of the same parking spot?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available and is preempted before marking it occupied.Step 2: Thread 2 also reads spot as available and proceeds to park, causing double-booking.
Both threads act on stale availability, leading to two vehicles occupying the same spot.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> double-booking [check]
Hint: Check availability -> context switch -> both park -> double-booking [check]
Common Mistakes:
3. The Elevator System currently calculates fees using a fixed rate. To add a
WeekendFeeStrategy 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) 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]
Hint: OCP: extend with new class, don't modify existing code [check]
Common Mistakes:
4. The system needs to support
ElectricCar playback devices that require ChargingSpot management. What is the minimum change to the current design to support this without modifying existing classes?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclassing allows extension without changing existing class code.Step 2: Update compatibility externally
Updating SpotCompatibility map outside existing classes respects OCP and avoids class explosion or god-class logic.Final Answer:
Option A -> Option AQuick Check:
Extend enums + subclass + external map update -> minimal change [check]
Hint: Extend enums + subclass + external map -> minimal change [check]
Common Mistakes:
5. To add an
ElectricPlayer who requires ChargingSpot in the Snake and Ladder game, what is the minimum change to the current design to maintain extensibility without modifying existing classes?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and new subclass preserves existing class code unchanged.Step 2: Update compatibility map for new player and spot types
SpotCompatibility map extension allows new rules without changing existing logic.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + compatibility map extension -> minimal existing code change [check]
Hint: New enums + subclass + map update = extensible addition [check]
Common Mistakes:
