Practice
SeatManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is it the correct choice?Solution
Step 1: Identify the pattern from constructor and method signatures
A private constructor prevents external instantiation, and a static synchronizedgetInstance()method ensures controlled single instance creation.Step 2: Reason why Singleton fits seat management
SeatManager must coordinate seat availability globally to avoid conflicting bookings, so a single shared instance is necessary.Final Answer:
Option D -> Option DQuick Check:
Private constructor + static synchronized getInstance() -> Singleton pattern [check]
LockerManager class has a private constructor and a static synchronized getInstance() method. Which design pattern is applied here and why is it the correct choice?Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single globally accessible instance.Step 2: Why Singleton fits this use case
LockerManager must coordinate locker assignments centrally to avoid conflicts, so a single instance is necessary. Factory or Builder do not enforce single instance, Prototype is for cloning, which is not needed here.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
getInstance() method returning a single instance. Which pattern is applied and why is this the CORRECT use case?Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single shared instance with thread safety.Step 2: Eliminate other creational patterns
Factory Method creates objects but doesn't enforce single instance; Builder constructs complex objects but not singleton; Prototype clones instances, not restricts to one.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized static getter -> Singleton pattern [check]
getInstance() method returning the single instance. Which design pattern is applied and why is this the CORRECT use case?Solution
Step 1: Identify the pattern by constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method controls access to a single instance, matching Singleton pattern characteristics.Step 2: Eliminate other patterns
Factory creates multiple instances, Builder constructs complex objects with multiple steps, and Prototype clones objects; none restrict to a single instance.Final Answer:
Option D -> Option DQuick Check:
Private constructor + synchronized static getter -> Singleton pattern [check]
execute() method is not synchronized, which exact sequence of events can cause the same command to be executed twice concurrently?Solution
Step 1: Identify the check-then-act race condition
Without synchronization, Thread 1 reads executed=false, then context switches before setting executed=true.Step 2: Thread 2 reads executed=false and proceeds to execute again
Both threads execute the command concurrently, causing double execution.Final Answer:
Option D -> Option DQuick Check:
Unsynchronized check-then-act allows double execution [check]
