Practice
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?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]
Plugin be an abstract class or an interface if plugins must maintain internal state and provide default lifecycle methods? Why?Solution
Step 1: Analyze state and default lifecycle method needs
Interfaces cannot hold instance state; abstract classes can hold fields and provide default method bodies.Step 2: Determine which construct fits stateful plugins with default behavior
Abstract class fits because it supports shared state and default lifecycle implementations, easing plugin development.Final Answer:
Option C -> Option CQuick Check:
Abstract class supports state and default methods [check]
Product be modeled as an abstract class or an interface? Consider that products have shared state like price and inventory count, but different behaviors like dispense() and getDescription().Solution
Step 1: Analyze shared state and behavior
Products share common state like price and inventory count, which requires fields and possibly default implementations.Step 2: Choose between interface and abstract class
Abstract classes can hold state and provide default method implementations, while interfaces cannot hold instance state (only constants).Final Answer:
Option A -> Option AQuick Check:
Abstract class supports shared state and behavior [check]
Solution
Step 1: Understand Java interface vs abstract class
Interfaces define behavior contracts without state, allowing multiple implementations. Abstract classes can hold state but restrict multiple inheritance.Step 2: Apply to iterator design
Iterator interface defines traversal methods (e.g.,hasNext(),next()) without state, enabling different iterator implementations per collection type.Final Answer:
Option D -> Option DQuick Check:
Iterator as interface enables flexible, stateless traversal contracts [check]
ElectricCar needing ChargingSpot in the vending machine parking design, what is the minimum change that respects extensibility without modifying existing classes?Solution
Step 1: Identify extensibility requirements
Adding new vehicle and spot types without modifying existing classes preserves OCP and avoids class explosion.Step 2: Evaluate options for minimal invasive change
AddELECTRIC_CARtoVehicleTypeenum,CHARGINGtoSpotTypeenum, createElectricCarsubclass, and updateSpotCompatibility.COMPATIBILITY_MAPaccordingly. adds new enum values and a subclass, updating compatibility map without changing existing classes. Others modify existing classes or add flags causing complexity.Final Answer:
Option B -> Option BQuick Check:
New enums and subclass extend design without modifying existing classes [check]
