Practice
1. In the Library Management System, should the
Book entity be designed as an abstract class or an interface, considering it has state like title and author and behavior like checkAvailability()? Why?easy
Solution
Step 1: Analyze state and behavior requirements
Book has common state (title, author) and behavior (checkAvailability) shared by all book types.Step 2: Abstract class vs interface for stateful entities
Abstract classes can hold state and provide default behavior; interfaces cannot hold instance state in Java.Final Answer:
Option B -> Option BQuick Check:
Stateful entity with shared behavior -> Abstract class [check]
Hint: State + behavior shared -> Abstract class, not interface [check]
Common Mistakes:
2. In designing a logging framework, should the
LogFormatter be an abstract class or an interface? Consider that formatters have no shared state but must implement a format(LogRecord) method. Why?easy
Solution
Step 1: Analyze formatter requirements
Formatters define a behavior (format method) without shared state or common implementation, making interface a natural fit.Step 2: Why interface over abstract class
Interfaces allow multiple implementations without forcing inheritance or shared state. Abstract classes imply shared state or default behavior, which is unnecessary here.Final Answer:
Option A -> Option AQuick Check:
No shared state + behavior only -> interface [check]
Hint: Behavior only, no state -> interface [check]
Common Mistakes:
3. In a multi-threaded parking lot system, if
parkVehicle() is not synchronized, which exact sequence of Thread 1 and Thread 2 calls causes double-booking of the same spot?medium
Solution
Step 1: Identify race condition
Without synchronization, both threads can read spot availability as true before either marks it occupied.Step 2: Trace exact interleaving
Thread 1: isAvailable() -> true, context switch; Thread 2: isAvailable() -> true; both proceed to park, causing double-booking.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap without synchronization -> double-booking [check]
Hint: Check availability then park must be atomic to avoid double-booking [check]
Common Mistakes:
4. You want to add a
WeekendFeeStrategy to the rate limiter's fee calculation without modifying existing classes. Which SOLID principle applies and what is the minimum change?medium
Solution
Step 1: Identify SOLID principle for adding new fee logic
Open-Closed Principle (OCP) requires adding new behavior without modifying existing code.Step 2: Evaluate options for minimal change respecting OCP and DIP
Add weekend flag to RateLimiter and use if-else inside existing fee method adds weekend flag and if-else inside existing method, violating OCP by modifying existing class. Create new WeekendFeeStrategy implementing FeeStrategy interface; inject it via constructor (OCP and DIP enforced) respects OCP and DIP by adding new class and injecting it.Final Answer:
Option A -> Option AQuick Check:
Minimal change but violates OCP by modifying existing class [check]
Hint: OCP: add new class, not modify existing code [check]
Common Mistakes:
5. In a parking lot system, if the
parkVehicle() method is NOT synchronized, which exact sequence of Thread 1 and Thread 2 causes double-booking of the same spot?medium
Solution
Step 1: Identify the race condition in check-then-act
Without synchronization, Thread 1 reads spot as available (true) but before it marks it occupied, context switches to Thread 2.Step 2: Thread 2 also reads spot as available and proceeds to park
Both threads proceed to park the vehicle, causing double-booking.Final Answer:
Option C -> Option CQuick Check:
Unsynchronized check-then-act -> both threads see available -> double-booking [check]
Hint: Unsynchronized check-then-act -> double-booking race [check]
Common Mistakes:
