Practice
MeetingManager 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 meeting schedules?Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()ensures a single shared instance, matching Singleton pattern characteristics.Step 2: Reason why Singleton fits MeetingManager
MeetingManager must coordinate all meetings centrally to avoid conflicting schedules; multiple instances could cause inconsistent state.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized getInstance() -> Singleton pattern [check]
User class has a private constructor and a static synchronized getInstance() method to manage user sessions. Which design pattern is applied here and why is this the correct choice?Solution
Step 1: Identify the pattern from constructor and method
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method controls instance creation, which matches Singleton pattern characteristics.Step 2: Why Singleton fits user session management
Singleton ensures a single global instance manages user sessions, avoiding multiple conflicting session managers.Final Answer:
Option A -> Option AQuick Check:
Private constructor + static synchronized getInstance() -> Singleton [check]
Vehicle abstraction that requires both state (e.g., speed) and behavior (e.g., accelerate()), should Vehicle be an abstract class or an interface in Java, and why?Solution
Step 1: Analyze state and behavior requirements
Vehicleneeds to hold state like speed and provide behavior like accelerate(), which requires shared fields and possibly implemented methods.Step 2: Choose between abstract class and interface
Abstract classes can hold instance variables and provide method implementations, while interfaces (pre-Java 8) cannot hold state and only declare methods. Even with default methods, interfaces cannot hold mutable state.Final Answer:
Option D -> Option DQuick Check:
State + behavior -> abstract class fits better than interface [check]
FeeStrategy interface for scoring rules. To add a WeekendBonusStrategy that modifies scoring only on weekends, what is the minimum change required to comply with SOLID principles?Solution
Step 1: Identify applicable SOLID principle
The Open-Closed Principle (OCP) requires that existing classes remain unchanged when adding new functionality.Step 2: Evaluate options against OCP
Add a newWeekendBonusStrategyclass implementingFeeStrategyand configure it without changing existing classes. adds a new class implementing the interface, extending behavior without modifying existing code, thus respecting OCP.Final Answer:
Option D -> Option DQuick Check:
New class implements interface -> no existing class modified -> OCP [check]
WeekendFeeStrategy for pricing in the current product catalog design, which change respects SOLID principles with minimum modification?Solution
Step 1: Identify applicable SOLID principle
The Open-Closed Principle (OCP) requires that classes be open for extension but closed for modification.Step 2: Why creating a new class is best
Implementing a newWeekendFeeStrategyclass extends functionality without modifying existing code, preserving OCP and avoiding side effects.Final Answer:
Option A -> Option AQuick Check:
New strategy class -> OCP compliance, no existing class changes [check]
