Practice
Vehicle abstraction for a parking system. Should Vehicle be an abstract class or an interface if you need to store state like licensePlate and provide default behavior for getSize()? Why?Solution
Step 1: Analyze state and behavior requirements
The design requires storing state (licensePlate) and providing default behavior (getSize()).Step 2: Choose correct OOP construct
Abstract classes can have instance variables and concrete methods with default behavior. Interfaces in Java 8+ can have default methods but cannot hold instance state. Therefore, abstract class fits the need.Final Answer:
Option C -> Option CQuick Check:
Abstract class supports state and default behavior [check]
Product be modeled as an abstract class or an interface? Consider that products have shared state like price and inventory count, but different behaviors like dispense() and getDescription().Solution
Step 1: Analyze shared state and behavior
Products share common state like price and inventory count, which requires fields and possibly default implementations.Step 2: Choose between interface and abstract class
Abstract classes can hold state and provide default method implementations, while interfaces cannot hold instance state (only constants).Final Answer:
Option A -> Option AQuick Check:
Abstract class supports shared state and behavior [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]
WeekendFeeStrategy to calculate weekend parking fees without modifying existing classes. Which SOLID principle guides the minimum change, and what is the correct approach?Solution
Step 1: Identify the SOLID principle for extensibility
Open/Closed Principle (OCP) requires adding new functionality without modifying existing code.Step 2: Evaluate approaches
Creating a new FeeStrategy implementation and injecting it respects OCP and DIP; modifying ParkingLot or existing strategies violates OCP or mixes responsibilities.Final Answer:
Option B -> Option BQuick Check:
New FeeStrategy class + injection -> OCP and DIP respected [check]
ElectricCar and ChargingSpot in your adapter-based parking system. What is the minimum change to the current design to support this without modifying existing classes?Solution
Step 1: Identify extensibility requirements
New vehicle and spot types must be added without modifying existing classes to respect OCP.Step 2: Evaluate Java-specific implementation
Adding new enum constants and new classes extends functionality cleanly; updating compatibility map centralizes logic without class changes.Step 3: Reject options modifying existing classes
Options B, C, and D modify existing classes or add flags, violating OCP and risking class explosion.Final Answer:
Option A -> Option AQuick Check:
New enums and classes added, existing classes unchanged [check]
