Practice
1. In the hotel system, should
Vehicle be designed as an abstract class or an interface, considering that different vehicle types have state like license plate and behavior like parking? Why?easy
Solution
Step 1: Analyze Vehicle's state and behavior
Vehicle has common state (license plate) and behavior (park, unpark) shared by all types.Step 2: Abstract class vs interface for state and behavior
Abstract classes can hold state and provide default behavior; interfaces cannot hold instance state (Java 8+ default methods don't hold state). Thus, abstract class fits better.Final Answer:
Option B -> Option BQuick Check:
Shared state + behavior -> abstract class [check]
Hint: Stateful base class -> abstract class, not interface [check]
Common Mistakes:
2. In designing the Airline Reservation System, should the
Seat entity be modeled as an abstract class or an interface, and why?easy
Solution
Step 1: Analyze seat characteristics
Seats have shared state (seat number, class) and common behavior (availability, booking), which suggests code reuse.Step 2: Abstract class vs interface
Abstract class allows shared state and default behavior implementation, while interface cannot hold state and only defines method signatures.Final Answer:
Option A -> Option AQuick Check:
Shared state + behavior -> abstract class [check]
Hint: Shared state needs abstract class, interfaces lack fields [check]
Common Mistakes:
3. A class
SecureImageProxy controls access to RealImage by checking user permissions before loading the image data lazily. Which design pattern is this an example of, and why is it the correct choice here?easy
Solution
Step 1: Identify the role of
This class controls access toSecureImageProxyRealImageand delays loading until necessary, matching Proxy's intent.Step 2: Eliminate other patterns
Decorator adds behavior without access control; Adapter changes interface; Facade simplifies interface but doesn't control access or lazy load.Final Answer:
Option A -> Option AQuick Check:
Proxy controls access and lazy loads expensive objects [check]
Hint: Proxy controls access and lazy loads objects [check]
Common Mistakes:
4. Adding a
WeekendFeeStrategy to the parking lot pricing requires which minimum change to comply with SOLID principles?medium
Solution
Step 1: Identify the SOLID principle involved
The Open/Closed Principle (OCP) requires that classes be open for extension but closed for modification.Step 2: Evaluate options against OCP
Add a boolean flagisWeekendtoParkingLotand handle fees internally. adds weekend logic insideParkingLot, modifying existing code and violating OCP. Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it without modifying existing classes. creates a new strategy class and injects it, adhering to OCP. Modify the existingFeeStrategyclass to add weekend logic with an if-statement. modifiesFeeStrategydirectly, violating OCP. ExtendParkingLotwith a subclassWeekendParkingLotthat overrides fee calculation. subclassesParkingLot, which can cause class explosion and violates single responsibility.Final Answer:
Option A -> Option AQuick Check:
Modifying ParkingLot internals violates OCP -> not minimal change [check]
Hint: OCP: extend with new class, don't modify existing code [check]
Common Mistakes:
5. To add support for
ElectricCar and ChargingSpot in the Chess game parking lot design, what is the minimum change that respects extensibility without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
New vehicle and spot types must be added without modifying existing classes to respect OCP.Step 2: Evaluate Java enum and class extension approach
Adding new enum constants and creating new subclasses externally allows extension without modifying existing code. Updating compatibility map externally avoids class explosion.Final Answer:
Option C -> Option CQuick Check:
Extends enums and classes externally, no existing class changes [check]
Hint: Extend enums + subclasses externally, no existing class edits [check]
Common Mistakes:
