Practice
Seat entity be modeled as an abstract class or an interface, and why?Solution
Step 1: Analyze seat characteristics
Seats have shared state (seat number, class) and common behavior (availability, booking), which suggests code reuse.Step 2: Abstract class vs interface
Abstract class allows shared state and default behavior implementation, while interface cannot hold state and only defines method signatures.Final Answer:
Option A -> Option AQuick Check:
Shared state + behavior -> abstract class [check]
SecureImageProxy controls access to RealImage by checking user permissions before loading the image data lazily. Which design pattern is this an example of, and why is it the correct choice here?Solution
Step 1: Identify the role of
This class controls access toSecureImageProxyRealImageand delays loading until necessary, matching Proxy's intent.Step 2: Eliminate other patterns
Decorator adds behavior without access control; Adapter changes interface; Facade simplifies interface but doesn't control access or lazy load.Final Answer:
Option A -> Option AQuick Check:
Proxy controls access and lazy loads expensive objects [check]
parkVehicle() checks if a spot is available and then marks it occupied. Without synchronized, which exact thread interleaving causes double-booking?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available but is preempted before marking it occupied. Thread 2 also reads available and proceeds to mark occupied.Step 2: Confirm double-booking scenario
Both threads believe the spot is free and mark it occupied, causing double-booking due to the race between check and set.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without synchronization -> double-booking [check]
ElectricCar and ChargingSpot in the existing parking system using Decorator pattern. What is the minimum change to support this without modifying existing classes and avoiding class explosion?Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot should not modify existing classes to respect OCP and avoid class explosion.Step 2: Evaluate options for minimal change
AddELECTRIC_CARtoVehicleType,CHARGINGtoSpotType, createElectricCarextendingVehicle, and updateSpotCompatibility.COMPATIBILITY_MAPaccordingly. adds new enum values and a subclass, updating compatibility map without changing existing classes, adhering to OCP. ModifyVehicleandSpotclasses to include electric-specific fields and logic. modifies existing classes, violating OCP. Create separate parallel class hierarchy for electric vehicles and spots, duplicating existing logic. duplicates logic, causing class explosion. Add booleanisElectricflag toVehicleandSpotand handle logic with conditionals. adds flags and conditionals, increasing complexity and violating SRP.Final Answer:
Option B -> Option BQuick Check:
New enums and subclass added; existing classes unchanged [check]
ElectricCar users who require ChargingSpot delivery locations. What is the minimum change to the current design to support this without modifying existing classes?Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclasses allows extension without changing existing class code, preserving OCP.Step 2: Understand how to update compatibility externally
Updating SpotCompatibility.COMPATIBILITY_MAP externally supports new types without modifying existing logic inside classes.Final Answer:
Option B -> Option BQuick Check:
Extend enums + subclasses + external map update -> minimal change [check]
