Practice
1. In designing the
User and Message classes for the messaging system, should User be an abstract class or an interface, and why?easy
Solution
Step 1: Analyze if User has state or just behavior
User holds common state such as userId and username, which requires fields and possibly default implementations.Step 2: Determine if abstract class or interface fits state and behavior
Abstract class supports shared state and partial implementation, while interface cannot hold instance state.Final Answer:
Option C -> Option CQuick Check:
User has common state -> abstract class fits better than interface [check]
Hint: Stateful base class -> abstract class, stateless -> interface [check]
Common Mistakes:
2. In the Restaurant Management System, should the
MenuItem be designed as an abstract class or an interface? Consider that MenuItem has shared state like price and description, and behavior like calculateDiscount().easy
Solution
Step 1: Analyze state and behavior requirements
MenuItem has shared state (price, description) and behavior (calculateDiscount), which interfaces cannot hold as fields.Step 2: Abstract class supports shared state and default behavior
Abstract classes can have instance variables and method implementations, making them suitable for MenuItem.Final Answer:
Option B -> Option BQuick Check:
Shared state + behavior -> abstract class [check]
Hint: Stateful base class -> abstract class, stateless contract -> interface [check]
Common Mistakes:
3. A class has a private constructor and a static synchronized
getInstance() method that returns the single instance. Which design pattern is applied here and why is this the CORRECT use case?easy
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]
Hint: Private constructor + static synchronized getInstance() -> Singleton [check]
Common Mistakes:
4. The notification system must support
ElectricCar vehicles that require ChargingSpot notifications. What is the minimum design change to support this 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: Apply minimal changes
Adding new enum values and creating new subclasses allows extension without modifying existing class code; updating compatibility map centralizes new logic.Step 3: Eliminate other options
Options B, C, and D modify existing classes or factory logic, violating OCP and risking class explosion or god-class anti-pattern.Final Answer:
Option C -> Option CQuick Check:
New enums and subclasses extend design without modifying existing classes [check]
Hint: Add enums + subclasses, avoid modifying existing classes [check]
Common Mistakes:
5. ElectricCar requires ChargingSpots in the parking lot system. What is the minimum change to support this without modifying existing classes?
hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and a subclass extends functionality without changing existing class code, respecting OCP.Step 2: Evaluate options
ModifyVehicleclass to include achargingRequiredflag and update all spot checks. modifies Vehicle, violating OCP. Create a newChargingSpotclass and modifyParkingLotto handle it specially. requires modifying ParkingLot, violating minimal change. Add a new interfaceChargeableand makeElectricCarimplement it, changing existing spot logic. adds interface but requires changing spot logic, violating minimal change.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + compatibility map update -> minimal change [check]
Hint: Add enums + subclass + compatibility map, no existing class change [check]
Common Mistakes:
