Practice
Vehicle be an abstract class or an interface, and why?Solution
Step 1: Identify if common state exists in Vehicle
Vehicle has shared fields like license plate, size, or type, which require storage and partial implementation.Step 2: Abstract class fits because it allows shared state and behavior, while interfaces cannot hold instance state.
Using an abstract class enforces common code reuse and prevents direct instantiation.Final Answer:
Option B -> Option BQuick Check:
Common state + shared behavior -> abstract class [check]
WeekendFeeStrategy to calculate parking fees differently on weekends using the current adapter-based design. What is the minimum change required to comply with SOLID principles?Solution
Step 1: Identify SOLID principle involved
Open-Closed Principle (OCP) requires extending behavior without modifying existing code.Step 2: Evaluate options
Create a new WeekendFeeStrategy class implementing FeeStrategy and inject it where needed without modifying existing classes. respects OCP by adding a new class implementing FeeStrategy. Add a boolean flag to Vehicle to indicate weekend and modify ParkingLot to check it. modifies Vehicle and ParkingLot, violating OCP. Options A and D modify existing classes.Final Answer:
Option A -> Option AQuick Check:
New strategy class extends behavior without modifying existing code [check]
Appender class has a writeLog() method that first checks if the log file is open, then writes the log. Without synchronization on writeLog(), which exact thread interleaving causes lost log entries?Solution
Step 1: Identify check-then-act race condition
Without synchronization, Thread 1 checks file open and sees true, then context switches before writing. Thread 2 also checks and sees true, both proceed to write concurrently.Step 2: Why this causes lost logs
Concurrent writes without synchronization cause race conditions, potentially overwriting or corrupting log entries.Final Answer:
Option B -> Option BQuick Check:
Check-then-act gap -> concurrent writes -> lost logs [check]
put() method is not synchronized, which exact sequence of operations between Thread 1 and Thread 2 can cause the cache to contain duplicate keys or corrupt the doubly linked list?Solution
Step 1: Identify the race condition in put()
Without synchronization, two threads can both check for key existence and find it missing before either inserts, leading to duplicate nodes for the same key.Step 2: Trace the exact interleaving
Thread 1: check key -> false; context switch; Thread 2: check key -> false; both insert new nodes, corrupting the cache state.Final Answer:
Option D -> Option DQuick Check:
Concurrent check-then-act without lock -> duplicate keys inserted [check]
parkVehicle() checks if a spot is available and then marks it occupied. Without synchronized, which exact thread interleaving causes double-booking?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads spot as available but is preempted before marking it occupied. Thread 2 also reads available and proceeds to mark occupied.Step 2: Explain how this leads to double-booking
Both threads believe the spot is free and mark it occupied independently, causing two vehicles to be assigned the same spot simultaneously.Final Answer:
Option A -> Option AQuick Check:
Check-then-act gap without synchronization -> double-booking [check]
