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]
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: Determine minimal change to add WeekendFeeStrategy
To avoid modifying existing classes, inject the new WeekendFeeStrategy via dependency injection, applying Dependency Inversion Principle (DIP) to depend on abstractions.Final Answer:
Option D -> Option DQuick Check:
Inject new strategy without modifying existing classes -> DIP [check]
WeekendFeeStrategy for charging players extra fees on weekends in the current Chess game design, which minimal change respects SOLID principles?Solution
Step 1: Identify SOLID principle involved
The Open-Closed Principle (OCP) requires the system to be open for extension but closed for modification.Step 2: Evaluate options against OCP
Add a boolean isWeekend flag to the Player class and check it during fee calculation. adds a flag to Player, allowing fee calculation to adapt without modifying FeeCalculator or creating new classes, thus minimally invasive.Final Answer:
Option A -> Option AQuick Check:
Adding flag avoids modifying existing fee classes [check]
WeekendFeeStrategy to calculate plugin usage fees on weekends without modifying existing plugin classes. Which SOLID principle guides the minimum change, and what is the correct approach?Solution
Step 1: Identify the SOLID principle for adding new behavior without modifying existing code
The Open-Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Apply OCP by creating a new WeekendFeeStrategy implementing FeeStrategy interface
This allows adding weekend fee logic without changing existing plugin or fee classes.Final Answer:
Option A -> Option AQuick Check:
OCP: extend with new strategy class, no existing class changes [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]
