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]
WeekendFeeStrategy to your logging framework's billing module without modifying existing classes. Which SOLID principle guides this design and what is the minimum change required?Solution
Step 1: Identify relevant SOLID principle
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Apply OCP to add WeekendFeeStrategy
Implement a new class WeekendFeeStrategy implementing FeeStrategy interface, so existing classes remain unchanged.Final Answer:
Option A -> Option AQuick Check:
New class implements interface -> existing classes unchanged -> OCP [check]
WeekendFeeStrategy for weekend pricing in the current booking system design, which minimal change respects the Open-Closed Principle (OCP)?Solution
Step 1: Identify OCP requirement
OCP requires adding new functionality without modifying existing tested code.Step 2: Evaluate options
Add a boolean flagisWeekendinBookingand handle fees there. adds weekend flag in Booking, which violates OCP by modifying Booking class. Create a new class implementingFeeStrategyinterface for weekend fees and inject it without changing existing classes. creates a new FeeStrategy class, respecting OCP by extension. Modify the existingFeeCalculatorclass to add weekend logic with if-else conditions. and D modify existing classes, violating OCP.Final Answer:
Option C -> Option CQuick Check:
New FeeStrategy class -> extend without modifying existing code [check]
ElectricCar vehicles requiring ChargingSpot compatibility. What is the minimum change to the current design to add 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: Use enums and subclassing
Adding new enum values and a new ElectricCar subclass extends functionality cleanly; updating compatibility map centralizes spot-vehicle matching without changing existing code.Step 3: Avoid modifying base classes or Executor
Modifying Vehicle or Executor violates OCP and risks introducing bugs.Final Answer:
Option D -> Option DQuick Check:
New enums + subclass + compatibility map update -> minimal, no existing class change [check]
ElectricCar vehicles requiring ChargingSpot types. What is the minimum change to the current Java design to add this without modifying existing classes?Solution
Step 1: Extend enums and create subclass
Adding new enum constants for vehicle and spot types allows type-safe extension without changing existing classes.Step 2: Update compatibility map externally
Updating SpotCompatibility.COMPATIBILITY_MAP to include ElectricCar and ChargingSpot relations extends behavior without modifying existing class code.Final Answer:
Option A -> Option AQuick Check:
Enum extension plus compatibility map update avoids modifying existing classes [check]
