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]
Vehicle abstraction for a parking system. Should Vehicle be an abstract class or an interface if you need to store state like licensePlate and provide default behavior for getSize()? Why?Solution
Step 1: Analyze state and behavior requirements
The design requires storing state (licensePlate) and providing default behavior (getSize()).Step 2: Choose correct OOP construct
Abstract classes can have instance variables and concrete methods with default behavior. Interfaces in Java 8+ can have default methods but cannot hold instance state. Therefore, abstract class fits the need.Final Answer:
Option C -> Option CQuick Check:
Abstract class supports state and default behavior [check]
TradeManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is this the correct choice?Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single instance is created and accessed thread-safely.Step 2: Why Singleton fits this scenario
TradeManager must coordinate all trades globally to maintain consistent state; multiple instances would cause conflicting trade states.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
Vehicle abstraction that requires both state (e.g., speed) and behavior (e.g., accelerate()), should Vehicle be an abstract class or an interface in Java, and why?Solution
Step 1: Analyze state and behavior requirements
SinceVehicleneeds to hold state like speed and provide default behavior likeaccelerate(), it requires fields and method implementations.Step 2: Choose between abstract class and interface
Java interfaces cannot hold instance state (fields) prior to Java 8 default methods, and even then, no instance variables. Abstract classes can hold state and provide default method implementations, making them suitable here.Final Answer:
Option C -> Option CQuick Check:
State + default behavior -> abstract class, not interface [check]
getInstance() method returning a single instance. Which pattern is applied and why is this the CORRECT use case?Solution
Step 1: Identify Singleton characteristics
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single shared instance with thread safety.Step 2: Eliminate other creational patterns
Factory Method controls subclass instantiation, Builder separates construction from representation, and Prototype clones objects; none enforce a single instance.Final Answer:
Option D -> Option DQuick Check:
Private constructor + static synchronized getInstance() -> Singleton pattern [check]
