Practice
1. A class has a private constructor and a static synchronized
getInstance() method returning a single instance. Which pattern is applied and why is this the CORRECT use case?easy
Solution
Step 1: Identify Singleton characteristics
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single shared instance with thread safety.Step 2: Eliminate other creational patterns
Factory Method controls subclass instantiation, Builder separates construction from representation, and Prototype clones objects; none enforce a single instance.Final Answer:
Option D -> Option DQuick Check:
Private constructor + static synchronized getInstance() -> Singleton pattern [check]
Hint: Private constructor + static getInstance() = Singleton [check]
Common Mistakes:
2. The Food Ordering System needs to add a
WeekendFeeStrategy to apply extra charges on weekends. Given the current design uses a FeeStrategy interface for pricing, what is the minimum change required to add this feature while respecting SOLID principles?medium
Solution
Step 1: Identify the SOLID principle involved
The Open-Closed Principle (OCP) requires that existing code should not be modified to add new behavior.Step 2: Evaluate options against OCP
Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it where needed without modifying existing classes. respects OCP by adding a new strategy class without modifying existing ones. Add a boolean weekend flag to theOrderclass and update all pricing logic accordingly. modifies Order class and pricing logic, violating OCP. Modify the existingFeeStrategyimplementation to add weekend logic with if-else checks. and D modify existing classes, violating OCP.Final Answer:
Option A -> Option AQuick Check:
New strategy class -> OCP respected, no existing class changes [check]
Hint: New strategy class -> respect OCP, avoid modifying existing code [check]
Common Mistakes:
3. The brokerage system currently calculates fees using a
FeeCalculator class. To add a new WeekendFeeStrategy without modifying existing code, which SOLID principle applies and what is the minimum change required?medium
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: How to apply OCP here
Create a new class WeekendFeeStrategy implementing FeeStrategy interface; FeeCalculator uses polymorphism to apply different strategies without modification.Final Answer:
Option A -> Option AQuick Check:
New strategy class + polymorphism -> OCP compliance [check]
Hint: Add new fee strategy class, no FeeCalculator changes [check]
Common Mistakes:
4. To support
ElectricCar needing ChargingSpot in the vending machine parking design, what is the minimum change that respects extensibility without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
Adding new vehicle and spot types without modifying existing classes preserves OCP and avoids class explosion.Step 2: Evaluate options for minimal invasive change
AddELECTRIC_CARtoVehicleTypeenum,CHARGINGtoSpotTypeenum, createElectricCarsubclass, and updateSpotCompatibility.COMPATIBILITY_MAPaccordingly. adds new enum values and a subclass, updating compatibility map without changing existing classes. Others modify existing classes or add flags causing complexity.Final Answer:
Option B -> Option BQuick Check:
New enums and subclass extend design without modifying existing classes [check]
Hint: Add enums + subclass + compatibility map, no existing class changes [check]
Common Mistakes:
5. You need to add support for
ElectricCar and ChargingSpot in an existing parking system using Observer pattern. What is the minimum change to the current design to support this without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot should not modify existing classes to respect OCP.Step 2: Evaluate options
Modify Vehicle and Spot classes to include electric-specific fields and logic for charging compatibility. and D modify existing classes, violating OCP; Create a new ElectricCar class and ChargingSpot class, then modify the Subject to notify these new types explicitly. modifies Subject, increasing coupling.Step 3: Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map externally. adds new enum values and subclasses, updating compatibility externally, preserving existing class integrity.
Final Answer:
Option D -> Option DQuick Check:
New enums and subclasses added externally -> existing classes unchanged [check]
Hint: Add enums and subclasses externally, keep existing classes untouched [check]
Common Mistakes:
