Practice
ElevatorController class uses a private constructor and a static synchronized getInstance() method to provide a single global access point. Which design pattern is applied here and why is it the correct choice?Solution
Step 1: Identify Singleton usage
The private constructor and static synchronizedgetInstance()method restrict instantiation to one object, ensuring a single ElevatorController.Step 2: Why Singleton fits ElevatorController
ElevatorController coordinates multiple elevators and floor queues centrally, so multiple instances would cause inconsistent state and scheduling conflicts.Final Answer:
Option B -> Option BQuick Check:
Singleton ensures one global ElevatorController instance [check]
ConnectionManager class uses a private constructor and a static synchronized getInstance() method to provide a single instance. Which design pattern is applied here, and why is this the correct choice for managing user connections?Solution
Step 1: Identify the pattern from constructor and method signature
The private constructor and static synchronizedgetInstance()method are classic signs of the Singleton pattern, ensuring a single instance.Step 2: Reason why Singleton fits connection management
Managing user connections globally requires a single shared instance to avoid inconsistent states or duplicated connection managers.Final Answer:
Option B -> Option BQuick Check:
Singleton ensures one global ConnectionManager instance [check]
Solution
Step 1: Understand Java interface vs abstract class
Interfaces define behavior contracts without state, allowing multiple implementations. Abstract classes can hold state but restrict multiple inheritance.Step 2: Apply to iterator design
Iterator interface defines traversal methods (e.g.,hasNext(),next()) without state, enabling different iterator implementations per collection type.Final Answer:
Option D -> Option DQuick Check:
Iterator as interface enables flexible, stateless traversal contracts [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]
ElectricCar and ChargingSpot in the existing parking system using the command pattern. What is the minimum change to the current design to support this without modifying existing classes?Solution
Step 1: Identify extensibility requirements
Adding new vehicle and spot types without modifying existing classes enforces OCP and avoids class explosion.Step 2: Evaluate options for minimal invasive change
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar extends Vehicle, and update SpotCompatibility.COMPATIBILITY_MAP accordingly adds new enum values and new class, updating compatibility map without changing existing classes. Options B, C, D modify existing classes or add complex logic violating OCP.Final Answer:
Option C -> Option CQuick Check:
New enums and class added, existing classes unchanged [check]
