Practice
BookingManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here, and why is this the correct choice for managing bookings?Solution
Step 1: Identify the pattern from constructor and method
A private constructor prevents external instantiation, and a static synchronizedgetInstance()method ensures a single shared instance, matching the Singleton pattern.Step 2: Reason why Singleton fits BookingManager
BookingManager must coordinate all bookings centrally to avoid conflicts; multiple instances would cause inconsistent state, so Singleton ensures global coordination.Final Answer:
Option A -> Option AQuick Check:
Private constructor + static synchronized getInstance() -> Singleton [check]
SecureImageProxy controls access to RealImage by checking user permissions before loading the image data lazily. Which design pattern is this an example of, and why is it the correct choice here?Solution
Step 1: Identify the role of
This class controls access toSecureImageProxyRealImageand delays loading until necessary, matching Proxy's intent.Step 2: Eliminate other patterns
Decorator adds behavior without access control; Adapter changes interface; Facade simplifies interface but doesn't control access or lazy load.Final Answer:
Option A -> Option AQuick Check:
Proxy controls access and lazy loads expensive objects [check]
Vehicle be modeled as an abstract class or an interface if it has state like speed and behavior like accelerate()? Why?Solution
Step 1: Analyze state and behavior requirements
Vehicle has state (speed) and behavior (accelerate()), so it needs a construct that supports instance variables and method implementations.Step 2: Evaluate abstract class vs interface
Abstract classes can hold state and provide default behavior; interfaces cannot hold instance state (only constants) and default methods can't maintain state.Final Answer:
Option B -> Option BQuick Check:
State + behavior with shared code -> abstract class [check]
scheduleJob() method is not synchronized, which exact thread interleaving causes the same Job to be scheduled twice?Solution
Step 1: Identify the check-then-act race condition
Both threads check if the job is already scheduled before adding. Without synchronization, both see job absent.Step 2: Trace thread interleaving
Thread 1 checks -> false, context switch before adding; Thread 2 checks -> false, adds job; Thread 1 resumes and adds job again, causing double scheduling.Final Answer:
Option C -> Option CQuick Check:
Check-then-act gap without synchronization -> double scheduling [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]
