Practice
Vehicle be an abstract class or an interface, and why?Solution
Step 1: Analyze Vehicle's shared state and behavior
Vehicle has common fields (e.g., licensePlate) and methods (e.g., startEngine) that require shared implementation and state storage.Step 2: Abstract class vs interface suitability
Abstract classes allow shared state and method implementations, while interfaces cannot hold instance state. Thus, abstract class fits Vehicle better.Final Answer:
Option A -> Option AQuick Check:
Shared state + behavior -> abstract class [check]
FitStrategy interface. To add a WeekendFitBonus that modifies scoring only on weekends, which minimal change respects SOLID principles?Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires adding new behavior without modifying existing classes.Step 2: Evaluate options for minimal change respecting OCP
Add a booleanisWeekendflag to theBoardclass and pass it to all fit calculations. adds a weekend flag to Board and passes it, allowing existing strategies to remain unchanged but adapt behavior based on context.Final Answer:
Option D -> Option DQuick Check:
Passing context flag avoids modifying existing FitStrategy classes [check]
WeekendRetryPolicy to the Job Scheduler without modifying existing classes. Which SOLID principle guides the minimum change, and what is the correct approach?Solution
Step 1: Identify the SOLID principle for extensibility
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Apply OCP to add WeekendRetryPolicy
Create a new class implementing RetryPolicy interface to add weekend-specific logic without modifying existing RetryPolicy or Job classes.Final Answer:
Option A -> Option AQuick Check:
New class implements interface -> no existing code changed [check]
ElectricCar vehicles requiring ChargingSpot parking. What is the minimum change to the current Java design to support this without modifying existing classes?Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot must not modify existing classes to respect OCP and avoid class explosion.Step 2: Minimal change approach
Adding new enum values and creating new subclasses for ElectricCar and ChargingSpot, plus updating compatibility externally, extends functionality without changing existing code.Final Answer:
Option A -> Option AQuick Check:
New enums + subclasses + external compatibility update -> no existing class change [check]
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]
