Practice
1. In a parking system, should
Vehicle be an abstract class or an interface if it needs to hold state like licensePlate and provide default behavior for getSize()? Why?easy
Solution
Step 1: Analyze state and behavior requirements
Vehicle needs to holdlicensePlate(state) and provide defaultgetSize()behavior, which interfaces cannot hold as instance variables.Step 2: Choose correct OOP construct
Abstract classes can hold state and provide default implementations, interfaces cannot hold instance state (only constants).Final Answer:
Option A -> Option AQuick Check:
State + default behavior -> abstract class, not interface [check]
Hint: State needs abstract class; interfaces can't hold instance variables [check]
Common Mistakes:
2. Given a
Vehicle abstraction that requires both state (e.g., speed) and behavior (e.g., accelerate()), should Vehicle be an abstract class or an interface in Java, and why?easy
Solution
Step 1: Analyze state and behavior requirements
SinceVehicleneeds to hold state like speed and provide default behavior likeaccelerate(), it requires fields and method implementations.Step 2: Choose between abstract class and interface
Java interfaces cannot hold instance state (fields) prior to Java 8 default methods, and even then, no instance variables. Abstract classes can hold state and provide default method implementations, making them suitable here.Final Answer:
Option C -> Option CQuick Check:
State + default behavior -> abstract class, not interface [check]
Hint: State needs fields -> abstract class; interfaces lack instance variables [check]
Common Mistakes:
3. You want to add a
WeekendFeeStrategy to calculate parking fees on weekends without modifying existing classes. Which SOLID principle guides this design and what is the minimum change?medium
Solution
Step 1: Identify applicable SOLID principles
Open/Closed Principle (OCP) requires extending behavior without modifying existing code; Dependency Inversion Principle (DIP) requires depending on abstractions likeFeeStrategy.Step 2: Apply minimal change preserving OCP and DIP
ImplementWeekendFeeStrategyas a new class implementingFeeStrategyand inject it where needed, avoiding changes to existing classes.Final Answer:
Option A -> Option AQuick Check:
New strategy class + injection -> OCP and DIP preserved [check]
Hint: New strategy class + inject -> OCP and DIP preserved [check]
Common Mistakes:
4. Facebook wants to add
ElectricCar users who require ChargingSpot support in the Vehicle and ParkingSpot hierarchy. 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 subclasses extends functionality without changing existing code.Step 2: Use external compatibility map update
Updating the compatibility map externally avoids modifying existing class logic, preserving OCP.Final Answer:
Option C -> Option CQuick Check:
New enums + subclass + external map update -> minimal change [check]
Hint: Add enums + subclass + external map, no existing class change [check]
Common Mistakes:
5. To add support for
ElectricCar needing ChargingSpot in the current parking lot design, what is the minimum change that respects extensibility and avoids modifying existing classes?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Enums are extended with new constants; new subclassElectricCaris created without changing existing classes.Step 2: Update compatibility map to include new vehicle and spot types
SpotCompatibility.COMPATIBILITY_MAPis updated to reflect which spots fitElectricCar, preserving OCP.Final Answer:
Option D -> Option DQuick Check:
Extend enums + new subclass + update compatibility map -> no existing class modified [check]
Hint: Extend enums + new subclass + update map -> no existing class changed [check]
Common Mistakes:
