Practice
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]
Solution
Step 1: Identify the role of state classes
Each state class implements the same interface but provides different behavior for the same method calls, which is polymorphism.Step 2: Eliminate other options
Inheritance alone does not explain different behaviors; encapsulation hides data but does not delegate behavior; abstraction defines interface but does not implement behavior.Final Answer:
Option D -> Option DQuick Check:
State classes override interface methods -> polymorphism [check]
FeeStrategy interface for pricing. To add a WeekendFeeStrategy that applies extra fees on weekends, which minimal change respects SOLID principles?Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires adding new behavior without modifying existing code.Step 2: Evaluate options
Add a boolean weekend flag to Order and modify FeeStrategy implementations to check it. modifies existing classes violating OCP; Modify the existing FeeStrategy implementation to check for weekends and apply extra fees. also modifies existing code; Add WeekendFeeStrategy as a new class implementing FeeStrategy and configure Order to use it without modifying existing classes. adds new class but requires Order modification; Create a WeekendFeeDecorator that wraps existing FeeStrategy implementations and adds weekend fees. adds decorator without modifying existing classes, respecting OCP and DIP.Final Answer:
Option D -> Option DQuick Check:
Decorator adds behavior without modifying existing classes [check]
getInstance(), what EXACT sequence of Thread 1 and Thread 2 calls can cause two instances to be created?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, both threads can see instance == null before either creates it.Step 2: Trace thread interleaving
Thread 1 checks instance == null -> true, context switch before creating instance. Thread 2 also checks instance == null -> true, proceeds to create a second instance, causing double instantiation.Final Answer:
Option A -> Option AQuick Check:
Unsynchronized lazy init -> check-then-act race causes two instances [check]
Solution
Step 1: Identify extensibility without modifying existing classes
To comply with OCP and avoid class explosion, new types should be added via enums and new subclasses without changing existing code.Step 2: Explain why Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map externally. fits minimal change
Add ELECTRIC_CAR to VehicleType enum, CHARGING to SpotType enum, create ElectricCar subclass, and update SpotCompatibility map externally. adds new enum values and creates a new ElectricCar subclass, updating compatibility externally, preserving existing classes unchanged. Options B, C, and D modify existing classes or create subclasses that duplicate logic, violating OCP or SRP.Final Answer:
Option D -> Option DQuick Check:
New enums + subclass + external compatibility update -> minimal change [check]
