Practice
1. In the Food Ordering System, should the
MenuItem be designed as an abstract class or an interface, considering it has both state (price, description) and behavior (calculateDiscount)? Why?easy
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]
Hint: State + behavior together -> abstract class, not interface [check]
Common Mistakes:
2. A class provides a
createIterator() method that returns an object allowing traversal over its elements without exposing its internal data structure. Which design pattern is being applied and why is this the correct choice?easy
Solution
Step 1: Identify the role of
This method returns an object that allows clients to traverse elements without exposing the collection's internal representation, which is the core intent of the Iterator pattern.createIterator()Step 2: Eliminate other patterns
Factory Method focuses on object creation, Decorator adds responsibilities dynamically, and Composite manages tree structures. None focus on traversal abstraction.Final Answer:
Option C -> Option CQuick Check:
Iterator separates traversal from collection internals [check]
Hint: Iterator hides structure, exposes traversal [check]
Common Mistakes:
3. You are designing a base class
Vehicle that provides a template method for starting the engine and driving, but also maintains internal state like fuel level. Should Vehicle be an abstract class or an interface, and why?easy
Solution
Step 1: Consider state management
SinceVehiclemaintains internal state like fuel level, it requires fields, which interfaces cannot hold.Step 2: Evaluate method implementations
Abstract classes can provide default implementations for template methods, supporting the Template Method pattern.Final Answer:
Option A -> Option AQuick Check:
Abstract class supports state and default behavior [check]
Hint: State needs abstract class, not interface [check]
Common Mistakes:
4. In a concurrent Job Scheduler, if the
scheduleJob() method is not synchronized, which exact thread interleaving causes the same Job to be scheduled twice?medium
Solution
Step 1: Identify the check-then-act race condition
Both threads check if the job is already scheduled before adding. Without synchronization, both see job absent.Step 2: Trace thread interleaving
Thread 1 checks -> false, context switch before adding; Thread 2 checks -> false, adds job; Thread 1 resumes and adds job again, causing double scheduling.Final Answer:
Option C -> Option CQuick Check:
Check-then-act gap without synchronization -> double scheduling [check]
Hint: Check-then-act gap -> double scheduling race [check]
Common Mistakes:
5. You need to add support for
ElectricCar and ChargingSpot in the existing parking system using the Iterator pattern. What is the minimum change to the current design to support this without modifying existing classes?hard
Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot should not require modifying existing classes to respect OCP.Step 2: Evaluate options
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map; existing classes remain unchanged. adds new enums and classes without modifying existing ones, preserving OCP. Modify Vehicle and Spot classes to include electric-specific fields and logic, then update iterators accordingly. modifies existing classes, violating OCP. Create a new ElectricVehicle interface extending Vehicle and a ChargingSpot class, then modify existing iterators to handle them. adds interfaces but requires modifying iterators, violating OCP. Add a flag isElectric to Vehicle class and modify SpotCompatibility to check this flag during iteration. modifies Vehicle class, violating OCP.Final Answer:
Option A -> Option AQuick Check:
Adding new enums and subclasses without changing existing classes respects OCP [check]
Hint: Add new types via enums and subclasses, no existing class changes [check]
Common Mistakes:
