0

Design Patterns and Best Practices

Description: Test your knowledge on Design Patterns and Best Practices.
Number of Questions: 15
Created by:
Tags: design patterns best practices software development
Attempted 0/15 Correct 0 Score 0

Which design pattern is used to define a family of algorithms, encapsulate each one, and make them interchangeable?

  1. Factory Method

  2. Strategy

  3. Observer

  4. Singleton


Correct Option: B
Explanation:

The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. This lets the algorithm vary independently from clients that use it.

What is the purpose of the Observer design pattern?

  1. To define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

  2. To create a single instance of a class and provide a global point of access to that instance.

  3. To provide a way to add or remove objects from a collection dynamically.

  4. To define an interface for creating objects, but let subclasses decide which class to instantiate.


Correct Option: A
Explanation:

The Observer pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Which design pattern is used to create a single instance of a class and provide a global point of access to that instance?

  1. Factory Method

  2. Strategy

  3. Observer

  4. Singleton


Correct Option: D
Explanation:

The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance.

What is the purpose of the Factory Method design pattern?

  1. To define a family of algorithms, encapsulate each one, and make them interchangeable.

  2. To create a single instance of a class and provide a global point of access to that instance.

  3. To provide a way to add or remove objects from a collection dynamically.

  4. To define an interface for creating objects, but let subclasses decide which class to instantiate.


Correct Option: D
Explanation:

The Factory Method pattern defines an interface for creating objects, but lets subclasses decide which class to instantiate.

Which design pattern is used to provide a way to add or remove objects from a collection dynamically?

  1. Factory Method

  2. Strategy

  3. Observer

  4. Composite


Correct Option: D
Explanation:

The Composite pattern composes objects into tree structures to represent part-whole hierarchies. It lets clients treat individual objects and compositions of objects uniformly.

What is the purpose of the Adapter design pattern?

  1. To convert the interface of a class into another interface clients expect.

  2. To define a family of algorithms, encapsulate each one, and make them interchangeable.

  3. To provide a way to add or remove objects from a collection dynamically.

  4. To define an interface for creating objects, but let subclasses decide which class to instantiate.


Correct Option: A
Explanation:

The Adapter pattern converts the interface of a class into another interface clients expect. This lets classes work together that couldn't otherwise because of incompatible interfaces.

Which design pattern is used to define a family of related classes and provide a way to create objects without specifying their exact class?

  1. Factory Method

  2. Abstract Factory

  3. Builder

  4. Prototype


Correct Option: B
Explanation:

The Abstract Factory pattern provides an interface for creating families of related objects without specifying their concrete classes.

What is the purpose of the Builder design pattern?

  1. To define a family of related classes and provide a way to create objects without specifying their exact class.

  2. To convert the interface of a class into another interface clients expect.

  3. To provide a way to add or remove objects from a collection dynamically.

  4. To define an interface for creating objects, but let subclasses decide which class to instantiate.


Correct Option: A
Explanation:

The Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations.

Which design pattern is used to provide a way to create objects without specifying their exact class?

  1. Factory Method

  2. Abstract Factory

  3. Builder

  4. Prototype


Correct Option: D
Explanation:

The Prototype pattern lets you create new objects by copying an existing object.

What is the purpose of the Decorator design pattern?

  1. To attach additional responsibilities to an object dynamically.

  2. To convert the interface of a class into another interface clients expect.

  3. To provide a way to add or remove objects from a collection dynamically.

  4. To define an interface for creating objects, but let subclasses decide which class to instantiate.


Correct Option: A
Explanation:

The Decorator pattern attaches additional responsibilities to an object dynamically. This lets you extend the functionality of an object without changing its structure.

Which design pattern is used to provide a way to undo or redo actions?

  1. Command

  2. Memento

  3. Observer

  4. Singleton


Correct Option: A
Explanation:

The Command pattern encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

What is the purpose of the Memento design pattern?

  1. To provide a way to undo or redo actions.

  2. To convert the interface of a class into another interface clients expect.

  3. To provide a way to add or remove objects from a collection dynamically.

  4. To define an interface for creating objects, but let subclasses decide which class to instantiate.


Correct Option: A
Explanation:

The Memento pattern provides a way to save and restore the state of an object without violating encapsulation.

Which design pattern is used to provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation?

  1. Iterator

  2. Composite

  3. Observer

  4. Singleton


Correct Option: A
Explanation:

The Iterator pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

What is the purpose of the Composite design pattern?

  1. To provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

  2. To convert the interface of a class into another interface clients expect.

  3. To provide a way to add or remove objects from a collection dynamically.

  4. To define an interface for creating objects, but let subclasses decide which class to instantiate.


Correct Option: C
Explanation:

The Composite pattern composes objects into tree structures to represent part-whole hierarchies. It lets clients treat individual objects and compositions of objects uniformly.

Which design pattern is used to provide a way to define a family of related classes and create objects without specifying their exact class?

  1. Factory Method

  2. Abstract Factory

  3. Builder

  4. Prototype


Correct Option: B
Explanation:

The Abstract Factory pattern provides an interface for creating families of related objects without specifying their concrete classes.

- Hide questions