Practice
1. In designing the ATM system, should the
Card entity be modeled as an abstract class or an interface, considering it has state (card number, expiry) and behavior (validate PIN)? Why?easy
Solution
Step 1: Analyze Card's characteristics
Card holds state (card number, expiry) and behavior (validate PIN), so it needs to share state and common logic.Step 2: Choose between abstract class and interface
Abstract class supports shared state and behavior inheritance. Interface cannot hold instance state, only constants and default methods without instance fields.Final Answer:
Option A -> Option AQuick Check:
Shared state + behavior -> abstract class [check]
Hint: Stateful base entity -> abstract class, not interface [check]
Common Mistakes:
2. In a multi-threaded adapter implementation, the method
parkVehicle() checks if a parking spot is available and then marks it occupied. Without synchronization, which exact thread interleaving causes double-booking?medium
Solution
Step 1: Trace parkVehicle() without synchronization
Thread 1 reads spot as free (isAvailable() -> true), then context switch occurs before marking occupied.Step 2: Thread 2 also reads spot as free and proceeds to mark occupied, causing double-booking.
Both threads believe spot is free and park simultaneously.Final Answer:
Option C -> Option CQuick Check:
Check-then-act race causes double-booking without synchronization [check]
Hint: Check-then-act gap causes double-booking without sync [check]
Common Mistakes:
3. In a multi-threaded logging framework, the
Appender class has a writeLog() method that first checks if the log file is open, then writes the log. Without synchronization on writeLog(), which exact thread interleaving causes lost log entries?medium
Solution
Step 1: Identify check-then-act race condition
Without synchronization, Thread 1 checks file open and sees true, then context switches before writing. Thread 2 also checks and sees true, both proceed to write concurrently.Step 2: Why this causes lost logs
Concurrent writes without synchronization cause race conditions, potentially overwriting or corrupting log entries.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap -> concurrent writes -> lost logs [check]
Hint: Check file open -> context switch -> both write -> race [check]
Common Mistakes:
4. In the Meeting Scheduler, the
RoomBookingManager class has a bookRoom() method that checks if a room is available and then marks it booked. Without synchronizing bookRoom(), which exact sequence of events between Thread 1 and Thread 2 causes a double-booking race condition?medium
Solution
Step 1: Identify check-then-act gap
Without synchronization, Thread 1 reads availability as true but is preempted before booking; Thread 2 also reads true and proceeds.Step 2: Both threads book simultaneously causing double-booking
Because the availability check and booking are not atomic, both threads can book the same room concurrently.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act -> double-booking race [check]
Hint: Check-then-act without sync -> double-booking race [check]
Common Mistakes:
5. You need to add ElectricCar support with ChargingSpots to a parking lot system using the State pattern. What is the minimum change to support this without modifying existing classes?
hard
Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot requires new types without modifying existing classes to respect OCP.Step 2: Evaluate options
Add ELECTRIC_CAR to VehicleType enum and CHARGING to SpotType enum, create ElectricCar subclass, update SpotCompatibility map adds new enum values and subclasses without changing existing classes, enabling extension. Options B, C, and D modify existing classes or interfaces, violating OCP.Final Answer:
Option C -> Option CQuick Check:
New enums and subclass -> no existing class modified [check]
Hint: Add new enums and subclasses, avoid modifying existing classes [check]
Common Mistakes:
