Practice
1. A class
CarBuilder constructs a Car object step-by-step using methods like setEngine(), setWheels(), and finally build(). The Car constructor is private. Which design pattern is applied here and why is it the correct choice?easy
Solution
Step 1: Identify the construction process
The class uses multiple setter methods to configure parts of the object before finalizing withbuild(), indicating stepwise construction.Step 2: Match pattern intent
Builder pattern separates construction from representation, ideal for complex objects with many optional parts. Factory Method uses inheritance, Prototype clones objects, Singleton restricts instance count.Final Answer:
Option A -> Option AQuick Check:
Stepwise object creation with private constructor -> Builder pattern [check]
Hint: Stepwise construction with private constructor -> Builder pattern [check]
Common Mistakes:
2. In the Elevator System, should the
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.easy
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]
Hint: Abstract class for shared state; interface for pure behavior [check]
Common Mistakes:
3. In a UML class diagram, should
Vehicle be modeled as an abstract class or an interface if it has state like speed and behavior like accelerate()? Why?easy
Solution
Step 1: Analyze state and behavior requirements
Vehicle has state (speed) and behavior (accelerate()), so it needs a construct that supports instance variables and method implementations.Step 2: Evaluate abstract class vs interface
Abstract classes can hold state and provide default behavior; interfaces cannot hold instance state (only constants) and default methods can't maintain state.Final Answer:
Option B -> Option BQuick Check:
State + behavior with shared code -> abstract class [check]
Hint: Stateful base class -> abstract class, not interface [check]
Common Mistakes:
4. The Car Rental System currently uses a
FeeStrategy interface for pricing. To add a WeekendFeeStrategy without modifying existing classes, which SOLID principle is being applied, and what is the minimum required change?medium
Solution
Step 1: Identify the SOLID principle for extensibility
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Determine minimal change to add WeekendFeeStrategy
To avoid modifying existing classes, inject the new WeekendFeeStrategy via dependency injection, applying Dependency Inversion Principle (DIP) to depend on abstractions.Final Answer:
Option D -> Option DQuick Check:
Inject new strategy without modifying existing classes -> DIP [check]
Hint: Add new strategy via injection, not modifying existing code [check]
Common Mistakes:
5. To support
ElectricCar and ChargingSpot in the Wallet System's parking module, what is the minimum change to the current design to maintain extensibility without modifying existing classes?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclasses externally allows extension without changing existing code, preserving OCP.Step 2: Use external compatibility map update
Updating SpotCompatibility.COMPATIBILITY_MAP externally avoids modifying existing classes, enabling new vehicle and spot types to integrate cleanly.Final Answer:
Option C -> Option CQuick Check:
New enums + subclass + external map update -> no existing class changes [check]
Hint: Add enums + subclass + external map update -> no existing class change [check]
Common Mistakes:
