Practice
1. Given a
Vehicle abstraction that requires both state (e.g., speed) and behavior (e.g., move()), should Vehicle be an abstract class or an interface? Why?easy
Solution
Step 1: Analyze state and behavior requirements
Vehicle needs to hold state like speed and provide behavior like move(), which requires shared fields and method implementations.Step 2: Evaluate abstract class vs interface
Abstract classes can hold instance variables and provide default method implementations; interfaces cannot hold instance state (only constants) despite default methods.Final Answer:
Option A -> Option AQuick Check:
State + behavior -> abstract class, not interface [check]
Hint: State needs abstract class; interfaces lack instance variables [check]
Common Mistakes:
2. A class has a private constructor and a static synchronized
getInstance() method returning the single instance. Which pattern is applied and why is this the CORRECT use case?easy
Solution
Step 1: Identify Singleton pattern characteristics
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single instance with thread-safe lazy initialization.Step 2: Eliminate other creational patterns
Factory Method controls subclass instantiation, Builder separates construction from representation, and Prototype clones objects; none enforce single instance or thread safety.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized static getInstance() -> Singleton pattern [check]
Hint: Private constructor + synchronized getInstance() -> Singleton [check]
Common Mistakes:
3. In a multi-threaded parking system using the Decorator pattern, the
parkVehicle() method checks if a spot is available and then marks it occupied. Without synchronization, which exact thread interleaving causes double-booking?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available (true) but before marking it occupied, context switches.Step 2: Trace Thread 2's actions
Thread 2 also reads spot as available (true) and proceeds to mark it occupied. Both threads think the spot is free and park vehicles, causing double-booking.Final Answer:
Option A -> Option AQuick Check:
Check-then-act without sync allows both threads to park simultaneously [check]
Hint: Check-then-act gap causes double-booking without sync [check]
Common Mistakes:
4. In the Library Management System, the
reserveBook() method is not synchronized. Which exact sequence of Thread 1 and Thread 2 calls causes double reservation of the same book copy?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, both threads can read availability as true before either updates the reservation status.Step 2: Trace thread interleaving causing double booking
Thread 1 reads available=true, context switch; Thread 2 reads available=true; both proceed to reserve, causing double booking.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> both see available=true -> double reservation [check]
Hint: Check availability -> context switch -> both reserve -> double booking [check]
Common Mistakes:
5. In a parking lot system, if
parkVehicle() is not synchronized, which exact thread interleaving causes double-booking of the same spot?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available but before marking it occupied, context switches to Thread 2 which also reads available.Step 2: Both threads proceed to park, causing double-booking
Both threads believe the spot is free and park simultaneously, violating mutual exclusion.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap without synchronization -> double-booking [check]
Hint: Check availability -> context switch -> both park -> double-booking [check]
Common Mistakes:
