Practice
1. In designing the
Post entity, should Post be an abstract class or an interface, considering it has state like content and timestamp? Why?easy
Solution
Step 1: Analyze state and behavior requirements
Posthas state fields likecontentandtimestamp, which require storage within the class.Step 2: Abstract class vs interface capabilities
Abstract classes can hold state and provide default method implementations, while interfaces (pre-Java 8) cannot hold state and primarily define behavior.Final Answer:
Option A -> Option AQuick Check:
Stateful entity -> abstract class preferred over interface [check]
Hint: Stateful entity? Use abstract class, not interface [check]
Common Mistakes:
2. A class has a private constructor and a
clone() method that returns a copy of itself. Which design pattern is applied here and why is this the correct use case?easy
Solution
Step 1: Identify the pattern from constructor and clone()
The private constructor prevents direct instantiation, and the presence of aclone()method indicates object copying rather than new creation.Step 2: Match pattern intent to scenario
The Prototype pattern is used to clone objects when construction is expensive, which fits the scenario. Singleton restricts to one instance, Factory creates new instances, and Builder separates construction steps.Final Answer:
Option D -> Option DQuick Check:
Private constructor + clone() -> Prototype pattern [check]
Hint: Private constructor + clone() means Prototype pattern [check]
Common Mistakes:
3. In a UML class diagram, should
Vehicle be modeled as an abstract class or an interface if it has state like speed and behavior like accelerate()? Why?easy
Solution
Step 1: Analyze state and behavior requirements
Vehicle has state (speed) and behavior (accelerate()), so it needs a construct that supports instance variables and method implementations.Step 2: Evaluate abstract class vs interface
Abstract classes can hold state and provide default behavior; interfaces cannot hold instance state (only constants) and default methods can't maintain state.Final Answer:
Option B -> Option BQuick Check:
State + behavior with shared code -> abstract class [check]
Hint: Stateful base class -> abstract class, not interface [check]
Common Mistakes:
4. In the Amazon Locker Service, if the
assignLocker() method is not synchronized, which exact sequence of thread interleaving causes two packages to be assigned the same locker?medium
Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads locker as available, then context switches before marking it occupied.Step 2: Thread 2 also reads locker as available and proceeds to assign it, causing double-booking.
Both threads assign the same locker before either updates the status.Final Answer:
Option C -> Option CQuick Check:
Check-then-act gap -> double assignment [check]
Hint: Check availability -> context switch -> both assign same locker [check]
Common Mistakes:
5. In a parking lot system, the
parkVehicle() method checks if a spot is available and then marks it occupied. Without synchronized, what 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 but before marking occupied, context switches to Thread 2 which also reads available.Step 2: Both threads proceed to mark the spot occupied independently
This leads to double-booking as both think the spot was free at check time.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap -> both threads see available -> double-booking [check]
Hint: Check-then-act without sync -> double-booking race [check]
Common Mistakes:
