Practice
Vehicle be designed as an abstract class or an interface if it needs to hold common state like licensePlate and rentalStatus? Why?Solution
Step 1: Analyze state requirements
Vehicle needs to hold common mutable state likelicensePlateandrentalStatus, which interfaces cannot hold as fields.Step 2: Choose correct OOP construct
Abstract classes can have instance variables and default method implementations, making them suitable for shared state. Interfaces cannot hold instance state, only constants.Final Answer:
Option D -> Option DQuick Check:
Shared mutable state -> abstract class, not interface [check]
FareCalculator class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is this the correct use case?Solution
Step 1: Identify the pattern from constructor and method signatures
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single shared instance, matching Singleton pattern characteristics.Step 2: Why Singleton fits FareCalculator
FareCalculator needs consistent fare rules and shared state (e.g., pricing configs). Singleton avoids multiple inconsistent instances and ensures thread-safe access.Final Answer:
Option C -> Option CQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
Solution
Step 1: Identify the role of the Doubly Linked List
The Doubly Linked List maintains the order of cache entries from most recently used to least recently used, enabling quick eviction of the least recently used item.Step 2: Match the pattern to this role
The Iterator pattern fits because it provides ordered traversal over the cache entries, allowing the system to efficiently move nodes and evict the least recently used.Final Answer:
Option A -> Option AQuick Check:
Doubly Linked List enables ordered traversal and eviction [check]
parkVehicle() method checks if a parking spot is available and then marks it occupied. Without synchronization, which exact thread interleaving causes double-booking of the same spot?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as free (isAvailable() -> true) but is preempted before marking it occupied.Step 2: Thread 2 also reads spot as free and proceeds to mark it occupied, causing double-booking.
This is the classic check-then-act gap causing race conditions.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
WeekendFeeStrategy to the parking lot pricing requires which minimum change to comply with SOLID principles?Solution
Step 1: Identify the SOLID principle involved
The Open/Closed Principle (OCP) requires that classes be open for extension but closed for modification.Step 2: Evaluate options against OCP
Add a boolean flagisWeekendtoParkingLotand handle fees internally. adds weekend logic insideParkingLot, modifying existing code and violating OCP. Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it without modifying existing classes. creates a new strategy class and injects it, adhering to OCP. Modify the existingFeeStrategyclass to add weekend logic with an if-statement. modifiesFeeStrategydirectly, violating OCP. ExtendParkingLotwith a subclassWeekendParkingLotthat overrides fee calculation. subclassesParkingLot, which can cause class explosion and violates single responsibility.Final Answer:
Option A -> Option AQuick Check:
Modifying ParkingLot internals violates OCP -> not minimal change [check]
