Practice
User and Message classes for the messaging system, should User be an abstract class or an interface, and why?Solution
Step 1: Analyze if User has state or just behavior
User holds common state such as userId and username, which requires fields and possibly default implementations.Step 2: Determine if abstract class or interface fits state and behavior
Abstract class supports shared state and partial implementation, while interface cannot hold instance state.Final Answer:
Option C -> Option CQuick Check:
User has common state -> abstract class fits better than interface [check]
ProductVariant interface versus an abstract class for an e-commerce catalog, which is the better choice and why?Solution
Step 1: Analyze state vs behavior sharing
Product variants share common state such as SKU, price, and inventory count, which is best encapsulated in an abstract class to avoid code duplication.Step 2: Why interface is less suitable
Interfaces define behavior contracts but cannot hold state, so using an interface alone would force each variant to reimplement common fields, violating DRY.Final Answer:
Option C -> Option CQuick Check:
Shared state -> abstract class; behavior only -> interface [check]
Elevator class implement an interface or extend an abstract class to represent different elevator types (e.g., passenger, freight)? Consider that elevators share common state and behavior but also have type-specific logic.Solution
Step 1: Analyze shared state and behavior
Elevators share common fields like currentFloor, direction, and doorStatus, plus some implemented methods.Step 2: Abstract class fits shared state and partial behavior
Abstract class allows code reuse and enforces common state, while subclasses override type-specific logic.Final Answer:
Option A -> Option AQuick Check:
Abstract class supports shared state and behavior [check]
Discount be designed as an abstract class or an interface? Consider that discounts have varying calculation logic but share no common state.Solution
Step 1: Analyze shared state vs behavior
Discounts vary in calculation logic and do not share common fields, so no shared state exists.Step 2: Choose interface for behavior without state
Interfaces define behavior contracts without enforcing state, fitting varying discount calculations better than abstract classes.Final Answer:
Option D -> Option DQuick Check:
No shared state + varying behavior -> 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]
