Practice
Cart class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here, and why is this the correct use case?Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method controls instance creation, which matches the Singleton pattern.Step 2: Reason why Singleton fits Cart
Ensuring only one Cart instance per user session avoids inconsistent states and duplicate carts, which is the intended use case here.Final Answer:
Option B -> Option BQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
WeekendFeeStrategy to the rate limiter's fee calculation without modifying existing classes. Which SOLID principle applies and what is the minimum change?Solution
Step 1: Identify SOLID principle for adding new fee logic
Open-Closed Principle (OCP) requires adding new behavior without modifying existing code.Step 2: Evaluate options for minimal change respecting OCP and DIP
Add weekend flag to RateLimiter and use if-else inside existing fee method adds weekend flag and if-else inside existing method, violating OCP by modifying existing class. Create new WeekendFeeStrategy implementing FeeStrategy interface; inject it via constructor (OCP and DIP enforced) respects OCP and DIP by adding new class and injecting it.Final Answer:
Option A -> Option AQuick Check:
Minimal change but violates OCP by modifying existing class [check]
FeeStrategy interface. 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 to fee calculation
Adding WeekendFeeStrategy as a new class implementing FeeStrategy extends behavior without modifying existing classes, preserving OCP.Final Answer:
Option A -> Option AQuick Check:
New FeeStrategy implementation -> no existing class changes -> OCP [check]
WeekendFeeStrategy to the parking lot pricing requires which minimum change to comply with SOLID principles?Solution
Step 1: Identify the SOLID principle involved
The Open-Closed Principle (OCP) requires that classes be open for extension but closed for modification.Step 2: Evaluate options against OCP
Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it without modifying existing classes. respects OCP by adding a new strategy class without modifying existing code. Add a booleanisWeekendflag toVehicleand change pricing logic accordingly. violates OCP by modifyingVehiclestate, which is unrelated to pricing and causes tight coupling.Final Answer:
Option B -> Option BQuick Check:
New strategy class -> OCP-compliant extension [check]
ElectricCar vehicles needing ChargingSpot parking. What is the minimum change to the current design to support this without modifying existing classes?Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclassing allows extension without changing existing class code, respecting OCP.Step 2: Update compatibility externally
UpdatingSpotCompatibilitymap outside existing classes avoids modifying their code, enabling new vehicle-spot matching.Final Answer:
Option D -> Option DQuick Check:
Extend enums + subclass + external map update -> no existing class changes [check]
