Practice
getInstance() method that returns the single instance. Which design pattern is applied here, and why is this the correct use case?Solution
Step 1: Identify the pattern from constructor and method signatures
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 creates objects but allows multiple instances; Builder constructs complex objects but doesn't restrict instance count; Prototype clones instances but doesn't enforce single instance.Final Answer:
Option B -> Option BQuick Check:
Private constructor + synchronized static getInstance() -> Singleton pattern [check]
Vehicle abstraction, should it be an abstract class or an interface if it needs to hold state like speed and fuelLevel? Why?Solution
Step 1: Analyze state and behavior requirements
SinceVehicleneeds to hold mutable state likespeedandfuelLevel, it requires fields and possibly default implementations that manage this state.Step 2: Compare abstract class vs interface capabilities
Abstract classes can hold instance variables and provide default method implementations that access state. Interfaces cannot hold instance state (only constants) and default methods cannot manage mutable state effectively.Final Answer:
Option C -> Option CQuick Check:
Stateful abstraction -> abstract class, not interface [check]
FeeCalculator class. To add a new WeekendFeeStrategy without modifying existing code, which SOLID principle applies and what is the minimum change required?Solution
Step 1: Identify SOLID principle for extensibility
Open/Closed Principle (OCP) requires adding new behavior without modifying existing classes.Step 2: Apply Dependency Inversion Principle (DIP)
Injecting the new WeekendFeeStrategy via constructor or setter allows FeeCalculator to depend on abstractions, enabling extension without modification.Final Answer:
Option A -> Option AQuick Check:
Inject new strategy -> respects OCP and DIP [check]
FeeStrategy interface for pricing. To add a WeekendFeeStrategy that applies extra fees on weekends, which minimal change respects SOLID principles?Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires adding new behavior without modifying existing code.Step 2: Evaluate options
Add a boolean weekend flag to Order and modify FeeStrategy implementations to check it. modifies existing classes violating OCP; Modify the existing FeeStrategy implementation to check for weekends and apply extra fees. also modifies existing code; Add WeekendFeeStrategy as a new class implementing FeeStrategy and configure Order to use it without modifying existing classes. adds new class but requires Order modification; Create a WeekendFeeDecorator that wraps existing FeeStrategy implementations and adds weekend fees. adds decorator without modifying existing classes, respecting OCP and DIP.Final Answer:
Option D -> Option DQuick Check:
Decorator adds behavior without modifying existing classes [check]
ElectricCar that requires ChargingSpot compatibility in the current parking system. What is the minimum change to the design to support this without modifying existing classes?Solution
Step 1: Identify extensibility requirements
ElectricCar and ChargingSpot are new types requiring compatibility mapping without changing existing classes.Step 2: Apply minimal invasive change
Adding new enum values and subclass plus updating compatibility map extends functionality without modifying existing class code, preserving OCP.Step 3: Avoid violating OCP
Options B, C, and D modify existing classes or create parallel hierarchies, increasing complexity and violating OCP.Final Answer:
Option B -> Option BQuick Check:
Extend enums and map, subclass ElectricCar -> minimal change [check]
