Practice
SeatManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is it the correct choice?Solution
Step 1: Identify the pattern from constructor and method signatures
A private constructor prevents external instantiation, and a static synchronizedgetInstance()method ensures controlled single instance creation.Step 2: Reason why Singleton fits seat management
SeatManager must coordinate seat availability globally to avoid conflicting bookings, so a single shared instance is necessary.Final Answer:
Option D -> Option DQuick Check:
Private constructor + static synchronized getInstance() -> Singleton pattern [check]
File and Directory classes, should FileSystemEntity be an abstract class or an interface? Why?Solution
Step 1: Identify shared state and behavior
Both File and Directory share common state such as name, permissions, timestamps, so a base class holding this state is needed.Step 2: Abstract class vs interface for state
Interfaces cannot hold instance fields; abstract classes can hold state and provide common method implementations, making abstract class the correct choice.Final Answer:
Option D -> Option DQuick Check:
Shared state -> abstract class, not interface [check]
Vehicle base type be an abstract class or an interface? Why?Solution
Step 1: Consider cloning and shared state
Cloning often requires copying internal state; an abstract class can hold this state and provide a defaultclone()implementation.Step 2: Evaluate interface limitations
Interfaces define behavior but cannot hold state or provide default cloning logic (in Java before default methods). Thus, abstract class fits better.Final Answer:
Option A -> Option AQuick Check:
Abstract class holds state + cloning logic -> correct OOP design [check]
WeekendFeeStrategy to calculate parking fees differently on weekends. Given the current design uses a FeeStrategy interface, what is the minimum change to comply with SOLID principles?Solution
Step 1: Identify SOLID principle
Open-Closed Principle (OCP) requires classes to be open for extension but closed for modification.Step 2: Apply OCP
Creating a newWeekendFeeStrategyclass extends functionality without modifying existing code, adhering to OCP and Dependency Inversion Principle.Final Answer:
Option C -> Option CQuick Check:
New strategy class -> extend without modifying existing -> OCP [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 to include new mappings supports new vehicle and spot types without class changes.Final Answer:
Option B -> Option BQuick Check:
Extend enums + subclass + update map -> no existing class modified [check]
