Practice
Vehicle base type be an abstract class or an interface? Why?Solution
Step 1: Consider cloning and shared state
Cloning often requires copying internal state; an abstract class can hold this state and provide a defaultclone()implementation.Step 2: Evaluate interface limitations
Interfaces define behavior but cannot hold state or provide default cloning logic (in Java before default methods). Thus, abstract class fits better.Final Answer:
Option A -> Option AQuick Check:
Abstract class holds state + cloning logic -> correct OOP design [check]
Board class has a placePiece() method that first checks if a position is free and then places the piece. Without synchronizing placePiece(), what exact sequence of thread interleaving causes two threads to place different pieces at the same position (double-booking)?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 checks position free and sees true, but before placing, context switches.Step 2: Thread 2 also checks position free -> true and proceeds to place piece concurrently, causing double-booking.
Final Answer:
Option A -> Option AQuick Check:
Unsynchronized check-then-act allows both threads to place pieces at same position [check]
Scorecard class has a recordBall() method that checks if the ball is valid and then updates the score. Without synchronizing recordBall(), what exact sequence of Thread 1 and Thread 2 causes double-counting of runs?Solution
Step 1: Identify check-then-act race condition
Both threads check ball validity before updating score; without synchronization, both see valid state.Step 2: Trace interleaving causing double-count
Thread 1 checks valid -> context switch; Thread 2 checks valid -> both proceed to update score independently causing double-count.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap without sync -> double update [check]
WeekendFeeStrategy to calculate weekend parking fees without modifying existing classes. Which SOLID principle guides the minimum change, and what is the correct approach?Solution
Step 1: Identify the SOLID principle for extensibility
Open/Closed Principle (OCP) requires adding new functionality without modifying existing code.Step 2: Evaluate approaches
Creating a new FeeStrategy implementation and injecting it respects OCP and DIP; modifying ParkingLot or existing strategies violates OCP or mixes responsibilities.Final Answer:
Option B -> Option BQuick Check:
New FeeStrategy class + injection -> OCP and DIP respected [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]
