Practice
Elevator class implement an interface or extend an abstract class to represent different elevator types (e.g., passenger, freight)? Consider that elevators share common state and behavior but also have type-specific logic.Solution
Step 1: Analyze shared state and behavior
Elevators share common fields like currentFloor, direction, and doorStatus, plus some implemented methods.Step 2: Abstract class fits shared state and partial behavior
Abstract class allows code reuse and enforces common state, while subclasses override type-specific logic.Final Answer:
Option A -> Option AQuick Check:
Abstract class supports shared state and behavior [check]
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
SinceVehicleneeds to hold state likelicensePlateand provide default behavior, it requires fields and method implementations.Step 2: Abstract class vs interface capabilities
Abstract classes can have instance fields and concrete methods, while interfaces cannot hold instance state (only constants) and default methods cannot manage instance variables.Final Answer:
Option C -> Option CQuick Check:
State + default behavior -> abstract class, not interface [check]
getInstance() method returning a single instance. Which pattern is applied and why is this the CORRECT use case?Solution
Step 1: Identify the pattern by constructor and method signature
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method returns a single shared instance, matching Singleton semantics.Step 2: Explain why Singleton is correct here
Singleton ensures a single instance with thread-safe lazy initialization via synchronization, which fits the described code. Factory Method and Builder patterns do not restrict instance count, and Prototype involves cloning, not single instance control.Final Answer:
Option B -> Option BQuick Check:
Private constructor + synchronized static getInstance() -> Singleton pattern [check]
WeekendFeeStrategy that charges extra on weekends, which minimal change respects SOLID principles?Solution
Step 1: Identify the SOLID principle involved
The Open-Closed Principle (OCP) requires classes to be open for extension but closed for modification.Step 2: Evaluate options against OCP
Create a newWeekendFeeStrategyclass implementingPricingStrategyand inject it without modifying existing classes. adds a new strategy class without modifying existing code, respecting OCP. Others modify existing classes or mix concerns.Final Answer:
Option A -> Option AQuick Check:
New strategy class extends pricing without modifying existing code [check]
WeekendFeeStrategy to the current parking fee system requires which minimum change to comply with SOLID principles?Solution
Step 1: Identify applicable SOLID principle
The Open/Closed Principle (OCP) requires that classes be open for extension but closed for modification.Step 2: Evaluate options against OCP
Create a newWeekendFeeStrategyclass implementingFeeStrategywithout modifying existing classes. respects OCP by adding a new class without modifying existing ones. Add a boolean flagisWeekendinParkingLotand conditionally apply fees inside existing strategies. violates OCP by modifyingParkingLotand existing strategies with conditional logic.Final Answer:
Option A -> Option AQuick Check:
New strategy class added, existing code unchanged -> OCP respected [check]
