Practice
final method that calls several abstract methods which subclasses must implement. Which design pattern is this an example of?Solution
Step 1: Identify the role of the final method
The base class'sfinalmethod defines the fixed algorithm skeleton, preventing subclasses from changing the overall flow.Step 2: Recognize abstract methods as extension points
Subclasses implement abstract methods to fill in specific steps, matching the Template Method pattern's intent.Final Answer:
Option A -> Option AQuick Check:
Base class controls algorithm flow; subclasses customize steps [check]
Vehicle be modeled as an abstract class or an interface if it has state like speed and behavior like accelerate()? Why?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]
movePlayer() method is not synchronized, which exact thread interleaving can cause two players to occupy the same board position simultaneously?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, both threads check if a position is free before moving.Step 2: Trace interleaving
Thread 1 checks position free -> true, context switch before moving; Thread 2 also checks -> true and moves; both occupy same position.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act causes double occupancy [check]
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclasses without changing existing classes respects OCP and avoids class explosion.Step 2: Update compatibility externally
Updating SpotCompatibility map outside existing classes allows new vehicle and spot types to integrate without modifying existing code.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + external compatibility update -> minimal change [check]
Solution
Step 1: Identify extensibility requirements
Adding ElectricCar and ChargingSpot must avoid modifying existing classes to respect OCP.Step 2: Evaluate options for minimal invasive change
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map externally adds new enum values and subclasses externally, updating compatibility map without changing existing code. Options B and C modify base classes, violating OCP. Create a god-class that handles both ElectricCar and ChargingSpot logic to avoid modifying existing classes creates god-class, violating SRP and maintainability.Final Answer:
Option C -> Option CQuick Check:
New enums + subclass + external map update -> minimal change, OCP respected [check]
