Practice
Vehicle be an abstract class or an interface, and why?Solution
Step 1: Identify if Vehicle has shared state
Vehicle has common fields like license plate, size, and type that need to be stored and reused.Step 2: Abstract class vs interface for shared state
Interfaces cannot hold state in Java (prior to default methods), so abstract class is needed to encapsulate shared state and partial implementation.Final Answer:
Option A -> Option AQuick Check:
Shared state in Vehicle -> abstract class needed [check]
WeekendFeedStrategy that changes the feed ranking on weekends, which minimal change respects SOLID principles in the current design where FeedService uses a FeedStrategy interface?Solution
Step 1: Identify applicable SOLID principle
Open/Closed Principle (OCP) requires new behavior added via new classes, not modifying existing ones.Step 2: Why new strategy class fits OCP and DIP
CreatingWeekendFeedStrategyimplementsFeedStrategyand can be injected, soFeedServiceremains unchanged and depends on abstractions.Final Answer:
Option C -> Option CQuick Check:
New strategy class + injection -> respects OCP and DIP [check]
makeMove() method is not synchronized. Which exact sequence of events between Thread 1 and Thread 2 can cause two players to place their marks on the same cell (double-booking)?Solution
Step 1: Identify the race condition in makeMove()
Without synchronization, the check-then-act sequence is vulnerable: Thread 1 checks if cell is empty and is preempted before marking.Step 2: Trace Thread 2's actions
Thread 2 also checks the same cell, sees it empty, and proceeds to mark it. Both threads mark the same cell, causing double-booking.Final Answer:
Option D -> Option DQuick Check:
Check-then-act gap without sync -> double-booking race [check]
ElectricCar needing ChargingSpot in the current parking lot design, what is the minimum change that respects extensibility and avoids modifying existing classes?Solution
Step 1: Identify extensibility without modifying existing classes
Enums are extended with new constants; new subclassElectricCaris created without changing existing classes.Step 2: Update compatibility map to include new vehicle and spot types
SpotCompatibility.COMPATIBILITY_MAPis updated to reflect which spots fitElectricCar, preserving OCP.Final Answer:
Option D -> Option DQuick Check:
Extend enums + new subclass + update compatibility map -> no existing class modified [check]
ElectricCar and ChargingSpot in an existing parking system using Observer pattern. What is the minimum change to the current design to support this without modifying existing classes?Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot should not modify existing classes to respect OCP.Step 2: Evaluate options
Modify Vehicle and Spot classes to include electric-specific fields and logic for charging compatibility. and D modify existing classes, violating OCP; Create a new ElectricCar class and ChargingSpot class, then modify the Subject to notify these new types explicitly. modifies Subject, increasing coupling.Step 3: Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map externally. adds new enum values and subclasses, updating compatibility externally, preserving existing class integrity.
Final Answer:
Option D -> Option DQuick Check:
New enums and subclasses added externally -> existing classes unchanged [check]
