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 the Online Auction System, the
AuctionManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is this the correct choice?easy
Solution
Step 1: Identify Singleton intent
The private constructor and static synchronizedgetInstance()method ensure only one instance ofAuctionManagerexists, centralizing auction state management.Step 2: Eliminate other patterns
Factory creates multiple objects, Builder incrementally builds complex objects, Prototype clones instances--none enforce single global instance.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
Hint: Private constructor + static getInstance() -> Singleton [check]
Common Mistakes:
3. In designing a collection class that supports iteration, should the iterator interface be an abstract class or an interface in Java, and why?
easy
Solution
Step 1: Understand Java interface vs abstract class
Interfaces define behavior contracts without state, allowing multiple implementations. Abstract classes can hold state but restrict multiple inheritance.Step 2: Apply to iterator design
Iterator interface defines traversal methods (e.g.,hasNext(),next()) without state, enabling different iterator implementations per collection type.Final Answer:
Option D -> Option DQuick Check:
Iterator as interface enables flexible, stateless traversal contracts [check]
Hint: Iterator interface defines behavior, no state needed [check]
Common Mistakes:
4. In the Restaurant Management System, the
TableManager class has a method reserveTable() that checks if a table is available and then marks it reserved. Without synchronizing reserveTable(), which exact sequence of thread interleaving causes double-booking?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads table as available but before reserving, Thread 2 also reads it as available.Step 2: Both threads proceed to reserve the same table
Because the reservation step is not atomic, both threads mark the table reserved, causing double-booking.Final Answer:
Option D -> Option DQuick Check:
Check-then-act without lock -> double-booking [check]
Hint: Check-then-act gap -> double-booking race [check]
Common Mistakes:
5. In a multi-threaded Order Management System, the
Order class has a changeState() method that checks if the current state allows transition and then updates the state without synchronization. Which exact thread interleaving can cause an invalid double state transition?medium
Solution
Step 1: Identify check-then-act race condition
Without synchronization, both threads read the same initial state before either updates it.Step 2: Trace thread interleaving
Thread 1 checks PLACED -> allowed to SHIPPED, context switch before update; Thread 2 also checks PLACED -> allowed to CANCELLED, both update state concurrently causing invalid double transition.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act allows concurrent conflicting state updates [check]
Hint: Unsynchronized check-then-act causes double state update [check]
Common Mistakes:
