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 abstraction, should it be an abstract class or an interface if it needs to hold common state like speed and fuelLevel? Why?Solution
Step 1: Identify need for shared state
SincespeedandfuelLevelare common state variables, the abstraction must hold state.Step 2: Choose between abstract class and interface
Interfaces cannot hold instance state (prior to Java 8 default methods, and even then no instance fields). Abstract classes can hold state and provide common behavior implementations.Final Answer:
Option A -> Option AQuick Check:
Shared state -> abstract class, not interface [check]
makeMove() method, which exact sequence of thread interleaving causes two players to move simultaneously, violating turn order?Solution
Step 1: Identify the check-then-act race condition
Both threads read the turn state before either updates it, causing both to believe it's their turn.Step 2: Trace thread interleaving
Thread 1 checks turn -> true, context switch before move; Thread 2 checks turn -> true, both move simultaneously, violating turn order.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap allows double move without synchronization [check]
WeekendFeeStrategy to the rate limiter's fee calculation without modifying existing classes. Which SOLID principle applies and what is the minimum change?Solution
Step 1: Identify SOLID principle for adding new fee logic
Open-Closed Principle (OCP) requires adding new behavior without modifying existing code.Step 2: Evaluate options for minimal change respecting OCP and DIP
Add weekend flag to RateLimiter and use if-else inside existing fee method adds weekend flag and if-else inside existing method, violating OCP by modifying existing class. Create new WeekendFeeStrategy implementing FeeStrategy interface; inject it via constructor (OCP and DIP enforced) respects OCP and DIP by adding new class and injecting it.Final Answer:
Option A -> Option AQuick Check:
Minimal change but violates OCP by modifying existing class [check]
WeekendFeeStrategy without modifying existing classes, which SOLID principle is being applied and what is the minimum required change?Solution
Step 1: Identify SOLID principle
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: Apply OCP by adding WeekendFeeStrategy implementing FeeStrategy interface without modifying existing classes.
Final Answer:
Option A -> Option AQuick Check:
New strategy class + injection -> OCP compliance [check]
