Practice
LogFormatter be an abstract class or an interface? Consider that formatters have no shared state but must implement a format(LogRecord) method. Why?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]
MeetingManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here, and why is this the correct choice for managing meeting schedules?Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()ensures a single shared instance, matching Singleton pattern characteristics.Step 2: Reason why Singleton fits MeetingManager
MeetingManager must coordinate all meetings centrally to avoid conflicting schedules; multiple instances could cause inconsistent state.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
parkVehicle() method checks if a spot is available and then marks it occupied. Without synchronization, 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 (true) but before marking it occupied, context switches.Step 2: Trace Thread 2's actions
Thread 2 also reads spot as available (true) and proceeds to mark it occupied. Both threads think the spot is free and park vehicles, causing double-booking.Final Answer:
Option A -> Option AQuick Check:
Check-then-act without sync allows both threads to park simultaneously [check]
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?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]
WeekendFeeStrategy to the existing pricing module of the in-memory SQL database's query cost estimation. Which minimum change respects the Open-Closed Principle (OCP)?Solution
Step 1: Understand OCP requirements
OCP requires software entities to be open for extension but closed for modification.Step 2: Evaluate options against OCP
Modify the existingFeeCalculatorclass to add weekend logic inside thecalculateFee()method. modifies existing class, violating OCP. Create a newWeekendFeeStrategyclass implementingFeeStrategyand inject it without changing existing classes. adds new strategy class without modifying existing code, respecting OCP. Add a booleanisWeekendflag to theFeeStrategyinterface and implement conditional logic in all strategies. adds flag to interface, forcing all implementations to change, violating OCP. Create a subclassWeekendFeeCalculatorextendingFeeCalculatorand overridecalculateFee(). subclasses but may require client changes, less flexible than strategy injection.Final Answer:
Option C -> Option CQuick Check:
New strategy class + injection -> OCP respected [check]
