Practice
MenuItem be designed as an abstract class or an interface, considering it has both state (price, description) and behavior (calculateDiscount)? Why?Solution
Step 1: Analyze state and behavior requirements
MenuItem holds state (price, description) and behavior (calculateDiscount) that should be shared or overridden.Step 2: Abstract class vs interface capabilities
Abstract classes can hold state and provide default method implementations; interfaces cannot hold instance state (before Java 8+ default methods, but still no fields).Final Answer:
Option A -> Option AQuick Check:
State + behavior with shared code -> Abstract class [check]
TransactionManager class has a processTransaction() method that checks if the ATM is available and then marks it busy. Without synchronization on this method, which exact thread interleaving causes double processing of transactions?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads isAvailable() as true but is preempted before marking busy.Step 2: Thread 2 also reads isAvailable() as true and proceeds, causing both to process simultaneously.
This is the classic check-then-act race leading to double processing.Final Answer:
Option B -> Option BQuick Check:
Check isAvailable true twice before marking busy -> double processing [check]
Scorecard class has a recordBall() method that checks if the ball is valid and then updates the score. Without synchronizing recordBall(), what exact sequence of Thread 1 and Thread 2 causes double-counting of runs?Solution
Step 1: Identify check-then-act race condition
Both threads check ball validity before updating score; without synchronization, both see valid state.Step 2: Trace interleaving causing double-count
Thread 1 checks valid -> context switch; Thread 2 checks valid -> both proceed to update score independently causing double-count.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without sync -> double update [check]
WinStrategy interface for win detection. To add a WeekendWinBonusStrategy that applies only on weekends, what is the minimum change required to comply with SOLID principles?Solution
Step 1: Understand OCP and DIP in current design
Open-Closed Principle (OCP) requires adding new behavior without modifying existing classes. Dependency Inversion Principle (DIP) encourages depending on abstractions.Step 2: Evaluate options for minimal change
Create a newWeekendWinBonusStrategyclass implementingWinStrategyand use it without modifying existing classes. adds a new strategy class but requires changing client code to use it, violating minimal change. Add a boolean flag in theBoardclass to toggle weekend mode and update all win checks accordingly. adds a toggle flag in Board, allowing existing strategies to adapt behavior without modifying their code, preserving OCP better than A or D.Final Answer:
Option B -> Option BQuick Check:
Toggle flag in Board -> minimal change, respects OCP [check]
parkVehicle() checks if a spot is available and then marks it occupied. Without synchronized, which exact thread interleaving causes double-booking?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available but is preempted before marking it occupied. Thread 2 also reads available and proceeds to mark occupied.Step 2: Explain how this leads to double-booking
Both threads believe the spot is free and mark it occupied independently, causing two vehicles to be assigned the same spot simultaneously.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap without synchronization -> double-booking [check]
