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]
Vehicle abstraction for a parking system, should Vehicle be an abstract class or an interface if it needs to hold state like licensePlate and provide default behavior for getSize()? Why?Solution
Step 1: Analyze state and behavior requirements
Vehicle needs to hold state (licensePlate) and provide default behavior (getSize), which interfaces cannot hold as fields.Step 2: Choose abstraction supporting state and default behavior
Abstract classes support instance variables and method implementations; interfaces cannot hold instance state.Final Answer:
Option A -> Option AQuick Check:
Abstract class supports state and default behavior -> correct choice [check]
WeekendFeeStrategy to calculate parking fees differently on weekends. According to SOLID principles, what is the minimum change required in the current design that uses a FeeStrategy interface?Solution
Step 1: Identify the SOLID principle involved
Open-Closed Principle (OCP) requires adding new behavior without modifying existing code.Step 2: Evaluate options for OCP compliance
Create a new WeekendFeeStrategy class implementing FeeStrategy and inject it where needed without modifying existing classes respects OCP by adding a new class; Add a boolean isWeekend flag to FeeStrategy interface and implement it in all classes violates OCP by modifying the interface; Modify the existing FeeStrategy implementation to add weekend logic with an if-statement modifies existing code; Create a WeekendFeeStrategy subclass extending the existing FeeStrategy implementation assumes FeeStrategy is a class, not interface.Final Answer:
Option B -> Option BQuick Check:
New strategy class added without changing existing code [check]
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclass ElectricCar extends Vehicle allows extension without changing existing class code.Step 2: Update compatibility map externally
Updating SpotCompatibility.COMPATIBILITY_MAP supports new types without modifying core logic, preserving OCP.Final Answer:
Option D -> Option DQuick Check:
Enum extension + subclass + external map update -> minimal change [check]
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclass ElectricCar extends Vehicle allows new types without changing existing code.Step 2: Update SpotCompatibility.COMPATIBILITY_MAP to include new compatibility rules for charging spots.
Final Answer:
Option D -> Option DQuick Check:
New enums and subclass added, existing classes unchanged -> extensible design [check]
