1.
Which of the following behavioural patterns sequentially access the elements of a collection?
Correct Answer
C. Iterator
Explanation
The correct answer is Iterator. The Iterator pattern provides a way to access the elements of a collection sequentially without exposing its underlying structure. It allows clients to iterate over a collection without knowing the specific implementation details. The Chain of Responsibility, Command, and Interpreter patterns do not specifically deal with the sequential access of elements in a collection.
2.
The behavioural pattern that defines simplified communication between classes is
Correct Answer
A. Mediator
Explanation
The correct answer is Mediator. The Mediator pattern defines a simplified communication between classes by introducing a mediator object that encapsulates the communication logic between these classes. It promotes loose coupling and reduces dependencies between classes, as they only need to communicate with the mediator instead of directly with each other. This pattern is useful in scenarios where a set of objects need to interact with each other in a controlled and centralized manner.
3.
The following are creational design patterns except:
Correct Answer
B. Adapter
Explanation
The given question is asking for the design pattern that is not a creational design pattern. The Factory Method, Abstract Factory, and Builder are all examples of creational design patterns as they provide ways to create objects. However, the Adapter design pattern is a structural design pattern, not a creational one. It is used to adapt the interface of one class to another, allowing them to work together.
4.
Behavioural patterns include the following except:
Correct Answer
D. Proxy
Explanation
Behavioural patterns in software design refer to patterns that focus on communication and interaction between objects. The Observer pattern allows objects to notify and update other objects about changes in their state. The State pattern allows an object to change its behavior based on its internal state. The Strategy pattern allows different algorithms or strategies to be used interchangeably within an object. However, the Proxy pattern is not a behavioural pattern. It is a structural pattern that provides a surrogate or placeholder for another object to control access to it.
5.
Which of the following creational design patterns creates a set of related objects or dependent objects?
Correct Answer
B. Abstract Factory
Explanation
The correct answer is Abstract Factory because the Abstract Factory design pattern is used to create a family of related objects or dependent objects. It provides an interface for creating objects, but allows subclasses to decide which class to instantiate. This pattern is useful when there are multiple families of objects that need to be created and the system should be independent of how these objects are created, composed, and represented.
6.
Which of these is not a structural design pattern?
Correct Answer
D. Visitor
Explanation
The Visitor pattern is not a structural design pattern. It is a behavioral design pattern. Structural design patterns deal with the composition of classes and objects to form larger structures, while behavioral design patterns focus on the interaction between objects and how they communicate with each other. The Adapter, Composite, and Proxy patterns are all examples of structural design patterns.
7.
Which of these design patterns allows an object to alter its behavior when its internal state changes?
Correct Answer
A. State
Explanation
The State design pattern allows an object to alter its behavior when its internal state changes. This pattern is used when an object needs to change its behavior based on its internal state, and it encapsulates each state into a separate class. The object delegates the behavior to the current state class, and when the state changes, the object's behavior changes accordingly. This pattern promotes loose coupling between the object and its states, making it easier to add or modify states without affecting the object's code.
8.
Proxy pattern falls under what design pattern?
Correct Answer
A. Structural
Explanation
The Proxy pattern falls under the Structural design pattern category. This pattern involves creating a proxy object that acts as a substitute for another object, controlling access to it and providing additional functionality if needed. The main purpose of the Proxy pattern is to provide a level of indirection between the client and the real object, allowing for easier control and management of the object's behavior.
9.
Which of these design patterns is used to process a list or chain of various types of request?
Correct Answer
C. Chain of Responsibility Design
Explanation
The Chain of Responsibility Design pattern is used to process a list or chain of various types of requests. In this pattern, multiple objects are linked together in a sequence, and each object has the ability to handle the request or pass it on to the next object in the chain. This allows for a flexible and dynamic way of handling requests, where each object in the chain can decide whether to handle the request or delegate it to the next object.
10.
The design pattern that involves the removal of an algorithm from its host class and putting it in a separate class is
Correct Answer
D. Strategy Design Pattern
Explanation
The correct answer is the Strategy Design Pattern. This design pattern involves removing an algorithm from its host class and placing it in a separate class. The host class then holds a reference to the strategy class and delegates the algorithm execution to it. This allows for easy swapping of different algorithms at runtime without modifying the host class.