CPP Design Patterns & Software Architecture 1 — Questions and Answers
Question 1: What is the Singleton design pattern?
- A pattern that creates a new object for each request
- A pattern ensuring a class has only one instance and provides a global access point to it (Correct answer)
- A pattern combining multiple objects into one
- A pattern for creating families of related objects
Correct answer: A pattern ensuring a class has only one instance and provides a global access point to it
The Singleton pattern restricts instantiation to one object and provides a static method to access that single instance.
Question 2: What is the Factory Method design pattern?
- A static method that initializes all application objects at startup
- A pattern defining an interface for creating an object but letting subclasses decide which class to instantiate (Correct answer)
- A pattern for building complex objects step by step
- A pattern caching and reusing objects
Correct answer: A pattern defining an interface for creating an object but letting subclasses decide which class to instantiate
The Factory Method pattern delegates object creation to subclasses, allowing a class to defer instantiation of the exact type it will use.
Question 3: What is the Observer design pattern?
- A pattern where one object monitors another's CPU usage
- A pattern defining a one-to-many dependency so that when one object changes state, all its dependents are notified automatically (Correct answer)
- A pattern for logging state changes to a file
- A pattern that watches for file system changes
Correct answer: A pattern defining a one-to-many dependency so that when one object changes state, all its dependents are notified automatically
The Observer pattern implements publish-subscribe, where a subject maintains a list of observers and notifies them of state changes.
Question 4: What is the Decorator design pattern?
- Adding comments and documentation to methods
- Attaching additional responsibilities to an object dynamically without modifying its class (Correct answer)
- Inheriting and overriding methods in a subclass
- Using annotations to inject dependencies
Correct answer: Attaching additional responsibilities to an object dynamically without modifying its class
The Decorator pattern wraps an object to extend its behavior at runtime, providing a flexible alternative to subclassing.
Question 5: What is layered (n-tier) architecture?
- Running multiple copies of the same application
- Organizing code into horizontal layers (presentation, business logic, data) where each layer only communicates with adjacent layers (Correct answer)
- A pattern using multiple inheritance hierarchies
- Deploying application components to multiple servers
Correct answer: Organizing code into horizontal layers (presentation, business logic, data) where each layer only communicates with adjacent layers
Layered architecture separates concerns into stacked layers, promoting maintainability and testability by isolating responsibilities.
Question 6: What is the Strategy design pattern?
- A pattern for choosing a database strategy
- A pattern defining a family of algorithms, encapsulating each one, and making them interchangeable (Correct answer)
- A pattern that selects a network protocol at compile time
- A pattern for handling exceptions using policies
Correct answer: A pattern defining a family of algorithms, encapsulating each one, and making them interchangeable
The Strategy pattern lets you select an algorithm's behavior at runtime by encapsulating each variant in its own class sharing a common interface.
What is the Singleton design pattern?