1.
Defines an interface for creating an object, but let the subclasses decide which class to instantiate. It let the instantiation differ to subclasses.
Correct Answer
A. Factory Method
Explanation
The Factory Method pattern defines an interface for creating an object, but allows subclasses to decide which class to instantiate. This pattern lets the instantiation differ to subclasses, meaning that each subclass can determine the specific object it wants to create. This promotes flexibility and extensibility in the code, as new subclasses can be easily added to create different types of objects without modifying the existing code.
2.
Attach additional responsibilities to an object dynamically. It provides a flexible alternative to subclassing for extending functionality.
Correct Answer
C. Decorator
Explanation
The Decorator pattern allows for the dynamic addition of responsibilities to an object at runtime, providing a flexible alternative to subclassing for extending functionality. This means that new behaviors can be added to an object without affecting its structure or the behavior of other objects in the system. The Decorator pattern is often used when there is a need to add or modify the behavior of an object without creating a new subclass.
3.
Define a family of algorithms, encapsulate each one, and make them interchangeable. It lets the algorithm vary independently from clients that use it.
Correct Answer
C. Strategy
Explanation
The Strategy pattern is the correct answer because it allows for the definition of a family of algorithms, encapsulation of each algorithm, and interchangeability between them. This pattern separates the algorithm implementation from the client code, allowing the algorithm to vary independently. The client can choose which algorithm to use at runtime, making it flexible and adaptable.
4.
Define one too many dependencies between objects so that when one object changes state, all its dependencies are notified and updated automatically.
Correct Answer
D. Observer
Explanation
The Observer pattern is used to define one too many dependencies between objects, where when one object changes state, all its dependencies are notified and updated automatically. This pattern allows for loose coupling between objects, as the subject (the object being observed) does not need to have direct knowledge of its observers. Instead, the subject maintains a list of observers and notifies them when its state changes. This allows for a flexible and decoupled system, where objects can react to changes in the subject without having to be tightly coupled to it.
5.
Encapsulate a request as an object, thereby letting you parametrize clients with different requests, queue or log requests, and support undoable operation.
Correct Answer
B. Command
Explanation
The given statement describes the Command design pattern. In this pattern, a request is encapsulated as an object, allowing clients to be parametrized with different requests. It also enables queuing or logging of requests and supports undoable operations. The Command pattern helps in decoupling the sender of a request from the receiver, providing flexibility and extensibility to the system.
6.
Ensure a class has only one instance, and provide a global access point to it.
Correct Answer
C. Singleton
Explanation
The correct answer is Singleton. Singleton is a design pattern that ensures a class has only one instance and provides a global access point to it. This pattern is useful when there should be only one instance of a class throughout the program and this instance needs to be accessible from different parts of the code. The Singleton pattern restricts the instantiation of a class to a single object and provides a way to access it globally.
7.
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. It lets subclasses redefine certain steps of an algorithm without changing the algorithm structure.
Correct Answer
B. Template method
Explanation
The given explanation correctly describes the concept of a template method. In the template method design pattern, the skeleton of an algorithm is defined in a base class, with certain steps deferred to subclasses. This allows subclasses to redefine or customize specific steps of the algorithm without altering the overall structure of the algorithm. This promotes code reuse and flexibility in designing related classes with similar behavior.
8.
Provide a unified interface to a set of interfaces in a subsystem. It defines a higher level interface that makes the subsystem easier to use.
Correct Answer
A. Facade
Explanation
A facade is a design pattern that provides a simplified interface to a complex system or set of interfaces. It acts as a unified interface that hides the complexities of the subsystem and makes it easier to use. It allows clients to interact with the subsystem through a single interface, instead of having to understand and interact with multiple interfaces. This simplifies the overall system architecture and improves usability.
9.
Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
Correct Answer
A. Iterator
Explanation
The Iterator pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. It allows clients to traverse the elements of a collection without knowing the specific structure of the collection. The Iterator interface defines methods like hasNext() to check if there are more elements, and next() to retrieve the next element in the sequence. This pattern promotes loose coupling between the client and the collection, as the client only needs to interact with the Iterator interface instead of the specific collection implementation.
10.
Which of the following is correct about the Singleton design pattern.
Correct Answer
D. All of the above.
Explanation
The given answer, "All of the above," is correct because all three statements mentioned in the question are true about the Singleton design pattern. The Singleton design pattern is a creational pattern, meaning it is used to create objects. It involves a single class that is responsible for creating an object and ensures that only one instance of the object is created. The Singleton class also provides a way to access its only object directly without needing to instantiate the class. Therefore, all three statements are correct.