Practice
Transaction class has a private constructor and a static synchronized getInstance() method that returns the single instance. Which design pattern is applied here and why is this the correct use case?Solution
Step 1: Identify the pattern from constructor and method signatures
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single instance is created and accessed thread-safely.Step 2: Why Singleton fits this Wallet Transaction scenario
Singleton is appropriate when exactly one instance is needed to coordinate actions or maintain global state, such as a ledger manager or transaction coordinator. Factory Method and Builder patterns do not enforce single instance, and Prototype is for cloning, not restricting instances.Final Answer:
Option B -> Option BQuick Check:
Private constructor + synchronized static getInstance() -> Singleton pattern [check]
Ball entity be modeled as an abstract class or an interface? Consider that Ball has state like ballNumber and behavior like calculateRuns(). Which is the best choice and why?Solution
Step 1: Analyze Ball's state and behavior
Ball has state (ballNumber) and behavior (calculateRuns), so it needs to store data and provide common logic.Step 2: Abstract class vs interface for state
Interfaces cannot hold instance state; abstract classes can hold state and provide shared implementation.Final Answer:
Option A -> Option AQuick Check:
Stateful entity with behavior -> abstract class [check]
Vehicle be an abstract class or an interface if it needs to hold state like licensePlate and provide default behavior for getSize()? Why?Solution
Step 1: Analyze state and behavior requirements
SinceVehicleneeds to hold state likelicensePlateand provide default behavior, it requires fields and method implementations.Step 2: Abstract class vs interface capabilities
Abstract classes can have instance fields and concrete methods, while interfaces cannot hold instance state (only constants) and default methods cannot manage instance variables.Final Answer:
Option C -> Option CQuick Check:
State + default behavior -> abstract class, not interface [check]
KitchenQueue class has a parkOrder() method that checks if a slot is available before assigning an order. Without synchronization on parkOrder(), which exact thread interleaving causes double-booking of the same slot?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads slot as available but is preempted before assignment; Thread 2 also reads available and proceeds.Step 2: Both threads assign the same slot leading to double-booking
Because the availability check and assignment are not atomic, both threads assign the same slot concurrently.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without sync -> double assignment [check]
castVote() method is not synchronized, which exact sequence of thread interleaving causes a double vote count for the same user on a question?Solution
Step 1: Identify the check-then-act race condition
The critical issue is the check if the user has already voted. Without synchronization, both threads see no vote recorded and proceed.Step 2: Trace exact interleaving causing double vote
Thread 1 checks voted -> false, context switch; Thread 2 checks voted -> false; both cast votes, causing double count.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without sync -> double vote [check]
