Practice
1. In a vehicle rental system, should
Vehicle be designed as an abstract class or an interface if it needs to hold common state like licensePlate and rentalStatus? Why?easy
Solution
Step 1: Analyze state requirements
Vehicle needs to hold common mutable state likelicensePlateandrentalStatus, which interfaces cannot hold as fields.Step 2: Choose correct OOP construct
Abstract classes can have instance variables and default method implementations, making them suitable for shared state. Interfaces cannot hold instance state, only constants.Final Answer:
Option D -> Option DQuick Check:
Shared mutable state -> abstract class, not interface [check]
Hint: Shared state needs abstract class, interfaces only define behavior [check]
Common Mistakes:
2. In the brokerage system, should the
Order entity be designed as an abstract class or an interface? Consider that orders have common state fields like orderId and timestamp, but different types of orders have distinct execution logic.easy
Solution
Step 1: Analyze state vs behavior requirements
Orders share common state fields likeorderIdandtimestamp, which require storage and possibly shared methods.Step 2: Why abstract class fits better
Abstract classes can hold state and provide default method implementations, reducing code duplication. Interfaces cannot hold instance state.Final Answer:
Option A -> Option AQuick Check:
Shared state + default behavior -> abstract class [check]
Hint: Stateful base class -> abstract class, not interface [check]
Common Mistakes:
3. Adding a
WeekendFeeStrategy to calculate weekend parking fees requires which minimum change to the current design that uses a FeeStrategy interface?medium
Solution
Step 1: Identify SOLID principle involved
The Open-Closed Principle (OCP) requires that new functionality be added by extending, not modifying, existing code.Step 2: Evaluate options against OCP
Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it where needed without modifying existing classes. respects OCP by adding a new class implementing the interface without changing existing code. Add a boolean flagisWeekendto theParkingLotclass and modify fee calculation logic accordingly. modifiesParkingLot, violating OCP. Options A and D modify or duplicate existing code, violating OCP or causing complexity.Final Answer:
Option D -> Option DQuick Check:
New strategy class + no existing code change -> OCP compliance [check]
Hint: OCP: extend with new class, don't modify existing code [check]
Common Mistakes:
4. ElectricCar pieces require special
ChargingSpot compatibility in the Jigsaw Puzzle System. What is the minimum design change to support ElectricCar and ChargingSpot without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
New piece and spot types must be added without modifying existing classes to respect OCP.Step 2: Evaluate minimal change approach
Adding new enum values and subclass for ElectricCarPiece plus updating compatibility map externally allows extension without modifying existing code.Final Answer:
Option A -> Option AQuick Check:
New enums and subclass extend system without changing existing classes [check]
Hint: Add enums + subclass + external map update = minimal invasive extension [check]
Common Mistakes:
5. ElectricCar requires ChargingSpots in the parking system using Prototype pattern. What is the minimum change to support this without modifying existing classes?
hard
Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot must avoid modifying existing classes to respect OCP.Step 2: Evaluate options for minimal invasive change
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map externally adds new enum values and subclasses externally, updating compatibility map without changing existing code. Options B and C modify base classes, violating OCP. Create a god-class that handles both ElectricCar and ChargingSpot logic to avoid modifying existing classes creates god-class, violating SRP and maintainability.Final Answer:
Option C -> Option CQuick Check:
New enums + subclass + external map update -> minimal change, OCP respected [check]
Hint: New enums + subclass + external map -> minimal invasive change [check]
Common Mistakes:
