Practice
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: Analyze state and behavior requirements
SinceVehicleneeds to hold common state likespeedandfuelLevel, it requires instance variables.Step 2: Choose abstraction supporting state
Abstract classes can have instance variables and shared method implementations, whereas interfaces cannot hold instance state (only constants and default methods without state).Final Answer:
Option B -> Option BQuick Check:
State requires instance variables -> abstract class [check]
Subject class maintains a list of Observer interfaces and notifies them on state changes. Which design pattern is this an example of, and why is it the correct choice for decoupling components?Solution
Step 1: Identify the pattern based on roles
TheSubjectmaintains a list ofObserverinterfaces and notifies them on state changes, which matches the Observer pattern's intent to decouple the subject from its observers.Step 2: Eliminate other patterns
Mediator centralizes communication but does not involve a subject notifying observers; Decorator adds behavior dynamically but does not handle notification; Strategy encapsulates algorithms but does not manage dependent objects.Final Answer:
Option A -> Option AQuick Check:
Subject notifying observers without knowing concrete classes -> Observer pattern [check]
FeeCalculator class. To add a new WeekendFeeStrategy without modifying existing code, which SOLID principle applies and what is the minimum change required?Solution
Step 1: Identify the SOLID principle for extensibility
Open/Closed Principle (OCP) states software entities should be open for extension but closed for modification.Step 2: How to apply OCP here
Create a new class WeekendFeeStrategy implementing FeeStrategy interface; FeeCalculator uses polymorphism to apply different strategies without modification.Final Answer:
Option A -> Option AQuick Check:
New strategy class + polymorphism -> OCP compliance [check]
selectItem() is not synchronized, which exact thread interleaving causes dispensing the same item twice?Solution
Step 1: Identify the check-then-act race
Without synchronization, both threads can read item availability as true before either decrements inventory.Step 2: Trace the interleaving
Thread 1 checks availability -> true, context switch; Thread 2 checks availability -> true; both proceed to dispense, causing double dispense.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act -> double dispense [check]
Solution
Step 1: Identify extensibility without modifying existing classes
Minimal change means no modification to existing classes; adding new enums or modifying compatibility map violates this.Step 2: Use composition or flags to extend behavior
Adding an isElectric boolean field to Vehicle and handling charging logic externally avoids class explosion and preserves OCP.Final Answer:
Option C -> Option CQuick Check:
Flag in Vehicle + external logic -> minimal change, no class modification [check]
