Practice
MeetingManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here, and why is this the correct choice for managing meeting schedules?Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()ensures a single shared instance, matching Singleton pattern characteristics.Step 2: Reason why Singleton fits MeetingManager
MeetingManager must coordinate all meetings centrally to avoid conflicting schedules; multiple instances could cause inconsistent state.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
Vehicle abstraction that requires both state (like speed) and behavior (like accelerate()). Should Vehicle be an abstract class or an interface, and why?Solution
Step 1: Analyze state and behavior requirements
Interfaces cannot hold instance state (fields) but can declare behavior. Abstract classes can hold state and provide default method implementations.Step 2: Evaluate options
Interface, because it can hold state and enforce behavior contracts. is incorrect because interfaces cannot hold state. Interface, because it allows multiple inheritance of state and behavior. is wrong as interfaces do not hold state. Abstract class, because it cannot have abstract methods. is false because abstract classes can have abstract methods.Final Answer:
Option C -> Option CQuick Check:
State + behavior -> abstract class, interfaces lack instance fields [check]
final method that calls several abstract methods which subclasses must implement. Which design pattern is this an example of?Solution
Step 1: Identify the role of the final method
The base class'sfinalmethod defines the fixed algorithm skeleton, preventing subclasses from changing the overall flow.Step 2: Recognize abstract methods as extension points
Subclasses implement abstract methods to fill in specific steps, matching the Template Method pattern's intent.Final Answer:
Option A -> Option AQuick Check:
Base class controls algorithm flow; subclasses customize steps [check]
ElectricCar and ChargingSpot in the Wallet System's parking module, what is the minimum change to the current design to maintain extensibility without modifying existing classes?Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclasses externally allows extension without changing existing code, preserving OCP.Step 2: Use external compatibility map update
Updating SpotCompatibility.COMPATIBILITY_MAP externally avoids modifying existing classes, enabling new vehicle and spot types to integrate cleanly.Final Answer:
Option C -> Option CQuick Check:
New enums + subclass + external map update -> 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]
