Practice
1. A class provides a
createIterator() method that returns an object allowing traversal over its elements without exposing its internal data structure. Which design pattern is being applied and why is this the correct choice?easy
Solution
Step 1: Identify the role of
This method returns an object that allows clients to traverse elements without exposing the collection's internal representation, which is the core intent of the Iterator pattern.createIterator()Step 2: Eliminate other patterns
Factory Method focuses on object creation, Decorator adds responsibilities dynamically, and Composite manages tree structures. None focus on traversal abstraction.Final Answer:
Option C -> Option CQuick Check:
Iterator separates traversal from collection internals [check]
Hint: Iterator hides structure, exposes traversal [check]
Common Mistakes:
2. A class has a private constructor and a static synchronized
getInstance() method returning the single instance. Which pattern is applied and why is this the CORRECT use case?easy
Solution
Step 1: Identify Singleton pattern characteristics
The private constructor prevents external instantiation, and the static synchronizedgetInstance()method ensures a single instance with thread-safe lazy initialization.Step 2: Eliminate other creational patterns
Factory Method controls subclass instantiation, Builder separates construction from representation, and Prototype clones objects; none enforce single instance or thread safety.Final Answer:
Option A -> Option AQuick Check:
Private constructor + synchronized static getInstance() -> Singleton pattern [check]
Hint: Private constructor + synchronized getInstance() -> Singleton [check]
Common Mistakes:
3. The system needs to support ElectricCar vehicles that require ChargingSpots. What is the minimum change to the current design to add this feature without modifying existing classes?
hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enums and subclasses externally allows extension without changing existing code, preserving OCP.Step 2: Update SpotCompatibility map externally
Maintaining compatibility mapping outside existing classes avoids class explosion or god-class logic.Final Answer:
Option D -> Option DQuick Check:
New enums + subclass + external compatibility update -> minimal change [check]
Hint: Add enums + subclass + external map update, no existing class edits [check]
Common Mistakes:
4. To add an
ElectricPlayer who requires ChargingSpot in the Snake and Ladder game, what is the minimum change to the current design to maintain extensibility without modifying existing classes?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and new subclass preserves existing class code unchanged.Step 2: Update compatibility map for new player and spot types
SpotCompatibility map extension allows new rules without changing existing logic.Final Answer:
Option A -> Option AQuick Check:
New enums + subclass + compatibility map extension -> minimal existing code change [check]
Hint: New enums + subclass + map update = extensible addition [check]
Common Mistakes:
5. You need to add support for
ElectricCar and ChargingSpot in the existing Template Method-based parking lot design. What is the minimum change to support this without modifying existing classes?hard
Solution
Step 1: Extend enums without modifying existing classes
Adding new enum values for vehicle and spot types allows new categories without changing existing code.Step 2: Create new subclass and update compatibility externally
ImplementingElectricCarsubclass and updating compatibility map outside existing classes respects OCP and avoids class explosion.Final Answer:
Option B -> Option BQuick Check:
New types and subclass added; existing classes unchanged [check]
Hint: Add enums and subclass; keep existing classes intact [check]
Common Mistakes:
