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 be an abstract class or an interface, considering vehicles have state like speed and direction? Why?Solution
Step 1: Analyze state vs behavior requirements
Vehicles have shared state (speed, direction) and behavior; interfaces cannot hold instance state, so interface alone is insufficient.Step 2: Abstract class encapsulates shared state and common methods
Abstract class allows shared fields and partial implementation, enabling subclasses to extend and customize behavior.Final Answer:
Option C -> Option CQuick Check:
Stateful base class -> abstract class, not interface [check]
Account class be an abstract class or an interface, considering it holds state like balance and transaction history? Why?Solution
Step 1: Analyze state and behavior requirements
The Account class holds mutable state (balance, transaction history) and common behavior (debit, credit), which requires shared implementation and fields.Step 2: Abstract class vs interface for stateful behavior
Interfaces cannot hold instance state (fields), only constants and default methods without backing fields. Abstract classes allow fields and method implementations, making them suitable for shared state and behavior.Final Answer:
Option D -> Option DQuick Check:
Stateful class with shared behavior -> Abstract class [check]
execute() method is not synchronized, which exact sequence of events can cause the same command to be executed twice concurrently?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads executed=false, then context switches before setting executed=true.Step 2: Thread 2 reads executed=false and proceeds to execute again
Both threads execute the command concurrently, causing double execution.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act allows double execution [check]
WeekendFeeStrategy to the current parking fee system requires which minimum change to comply with SOLID principles?Solution
Step 1: Identify applicable SOLID principle
The Open/Closed Principle (OCP) requires that classes be open for extension but closed for modification.Step 2: Evaluate options against OCP
Create a newWeekendFeeStrategyclass implementingFeeStrategywithout modifying existing classes. respects OCP by adding a new class without modifying existing ones. Add a boolean flagisWeekendinParkingLotand conditionally apply fees inside existing strategies. violates OCP by modifyingParkingLotand existing strategies with conditional logic.Final Answer:
Option A -> Option AQuick Check:
New strategy class added, existing code unchanged -> OCP respected [check]
