Practice
1. In a vehicle rental system, should
Vehicle be designed as an abstract class or an interface if it needs to hold common state like licensePlate and rentalStatus? Why?easy
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]
Hint: Shared state needs abstract class, interfaces only define behavior [check]
Common Mistakes:
2. A class
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?easy
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]
Hint: Proxy controls access and lazy loads objects [check]
Common Mistakes:
3. In the Food Ordering System, the
KitchenQueue class has a parkOrder() method that checks if a slot is available before assigning an order. Without synchronization on parkOrder(), which exact thread interleaving causes double-booking of the same slot?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads slot as available but is preempted before assignment; Thread 2 also reads available and proceeds.Step 2: Both threads assign the same slot leading to double-booking
Because the availability check and assignment are not atomic, both threads assign the same slot concurrently.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without sync -> double assignment [check]
Hint: Check availability -> context switch -> both assign same slot [check]
Common Mistakes:
4. To add
ElectricCar needing ChargingSpot support, what is the minimum change to the current design without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot support must avoid modifying existing classes to comply with OCP and maintain code modularity.Step 2: Evaluate options for minimal invasive change
AddELECTRIC_CARtoVehicleType,CHARGINGtoSpotType, createElectricCarextendingVehicle, and updateSpotCompatibility.COMPATIBILITY_MAPaccordingly. extends enums and adds new classes, updating compatibility map without changing existing classes. Options B, C, and D modify existing classes or add god-class logic, violating OCP and increasing complexity.Step 3: Confirm Java-specific implementation details
Updating enums and compatibility map is idiomatic in Java for extensibility without class explosion.Final Answer:
Option A -> Option AQuick Check:
Extend enums + new class + compatibility map update -> minimal change [check]
Hint: Extend enums + new class + compatibility map -> minimal invasive change [check]
Common Mistakes:
5. You need to add support for
ElectricCar and ChargingSpot in the parking system without modifying existing classes. What is the minimum change to the current design?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Enums can be extended with new constants, and new subclasses can be added without changing existing code.Step 2: Use external compatibility map update
Updating the compatibility map externally avoids modifying existing classes, preserving OCP and avoiding class explosion.Step 3: Avoid flags or interface checks that require existing class changes
Flags or interface checks require modifying existing classes, violating OCP.Final Answer:
Option B -> Option BQuick Check:
Extend enums + new subclass + external map update -> minimal change [check]
Hint: Extend enums + subclass + external map -> minimal change [check]
Common Mistakes:
