Practice
Package entity be modeled as an abstract class or an interface, and why?Solution
Step 1: Analyze Package state and behavior
Package has shared mutable state such as OTP, expiry time, and tracking info, which requires fields and common method implementations.Step 2: Abstract class vs interface suitability
Abstract classes can hold state and provide default behavior, interfaces cannot hold mutable state (prior to Java 8 default methods, but still no fields). Therefore, abstract class fits better.Final Answer:
Option B -> Option BQuick Check:
Shared mutable state -> abstract class needed [check]
FeeStrategy interface for pricing. To add a WeekendFeeStrategy without modifying existing classes, which SOLID principle is being applied and what is the minimum required change?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 by adding new strategy class
ImplementWeekendFeeStrategyas a new class implementingFeeStrategyinterface, no changes to existing classes needed.Final Answer:
Option A -> Option AQuick Check:
New class implements interface -> existing code unchanged -> OCP [check]
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enums and subclasses externally allows extension without changing existing code, preserving OCP.Step 2: Update SpotCompatibility map externally
Maintaining compatibility mapping outside existing classes avoids class explosion or god-class logic.Final Answer:
Option D -> Option DQuick Check:
New enums + subclass + external compatibility update -> minimal change [check]
Solution
Step 1: Identify extensibility requirements
We want to add ElectricCar and ChargingSpot without modifying existing classes to respect OCP.Step 2: Evaluate options
Modify Vehicle base class to include a boolean isElectric field and add logic in Spot class to check this field. modifies existing Vehicle class, violating OCP. Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map; no existing classes modified. adds enum values and compatibility map without changing existing classes, preserving extensibility.Step 3: Confirm minimal change
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map; no existing classes modified. adds new enum constants and new subclass, updating compatibility map externally, no existing class code changed.Final Answer:
Option A -> Option AQuick Check:
New enums and subclass added, existing classes untouched [check]
ElectricCar players who require ChargingSpot betting spots. What is the minimum design change to support this without modifying existing classes?Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclasses externally allows extension without changing existing code, respecting OCP.Step 2: Why other options violate OCP or cause class explosion
Options B and D modify existing classes, violating OCP; Create a newElectricCarVehicleandChargingSpotclasses inheriting from existing ones and modify base classes to support them creates new classes but requires base class changes.Final Answer:
Option A -> Option AQuick Check:
Extend enums + subclass + external compatibility map -> minimal change [check]
