Practice
Reputation system for Stack Overflow, should Reputation be an abstract class or an interface, and why?Solution
Step 1: Analyze Reputation's need for state and behavior
Reputation tracks mutable points and provides default methods like addPoints(), so it requires state and behavior.Step 2: Abstract class vs interface capabilities
Abstract classes can hold state and provide default implementations; interfaces cannot hold instance state (prior to Java 8 default methods, but still no fields).Final Answer:
Option A -> Option AQuick Check:
Mutable state + default behavior -> Abstract class [check]
WeekendBonusRule that gives players extra moves on weekends without modifying existing classes. Which SOLID principle guides the minimal change, and what is the correct approach?Solution
Step 1: Identify SOLID principle for extension without modification
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Evaluate approaches
Subclassing Player to WeekendPlayer allows extension without modifying Player; injecting new MoveRule class also respects OCP but requires interface design.Final Answer:
Option C -> Option CQuick Check:
Subclassing extends behavior without modifying base class [check]
WeekendFeeStrategy to the existing pricing module of the in-memory SQL database's query cost estimation. Which minimum change respects the Open-Closed Principle (OCP)?Solution
Step 1: Understand OCP requirements
OCP requires software entities to be open for extension but closed for modification.Step 2: Evaluate options against OCP
Modify the existingFeeCalculatorclass to add weekend logic inside thecalculateFee()method. modifies existing class, violating OCP. Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it without changing existing classes. adds new strategy class without modifying existing code, respecting OCP. Add a booleanisWeekendflag to theFeeStrategyinterface and implement conditional logic in all strategies. adds flag to interface, forcing all implementations to change, violating OCP. Create a subclassWeekendFeeCalculatorextendingFeeCalculatorand overridecalculateFee(). subclasses but may require client changes, less flexible than strategy injection.Final Answer:
Option C -> Option CQuick Check:
New strategy class + injection -> OCP respected [check]
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclasses without changing existing classes respects OCP and avoids class explosion.Step 2: Update compatibility externally
Updating SpotCompatibility map outside existing classes allows new vehicle and spot types to integrate without modifying existing code.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + external compatibility update -> minimal change [check]
ElectricCar and ChargingSpot in the existing parking system using the Iterator pattern. What is the minimum change to the current design to support this without modifying existing classes?Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot should not require modifying existing classes to respect OCP.Step 2: Evaluate options
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map; existing classes remain unchanged. adds new enums and classes without modifying existing ones, preserving OCP. Modify Vehicle and Spot classes to include electric-specific fields and logic, then update iterators accordingly. modifies existing classes, violating OCP. Create a new ElectricVehicle interface extending Vehicle and a ChargingSpot class, then modify existing iterators to handle them. adds interfaces but requires modifying iterators, violating OCP. Add a flag isElectric to Vehicle class and modify SpotCompatibility to check this flag during iteration. modifies Vehicle class, violating OCP.Final Answer:
Option A -> Option AQuick Check:
Adding new enums and subclasses without changing existing classes respects OCP [check]
