1. Home
  2. Explore
  3. Computer Science
  4. Design Patterns Overview

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Design Patterns Overview

  • 73Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Design Patterns Overview

What is the primary purpose of design patterns in software engineering?

73 questions

  1. 1

    What is the primary purpose of design patterns in software engineering?

    1. A

      They are well-tested, reusable solutions to common software engineering problems.

    2. B

      They are visual templates for designing user interfaces.

    3. C

      They are mandatory coding standards that all developers must follow.

    4. D

      They are specific algorithms for sorting and searching data.

    Correct answer
    A

    They are well-tested, reusable solutions to common software engineering problems.

    Explanation

    Design patterns are defined as well-tested, reusable solutions to common problems, providing a template for solving software engineering issues.

  2. 2

    Which of the following best describes how design patterns facilitate communication among developers?

    1. A

      They provide a common meta-language that developers can use to understand each other's code.

    2. B

      They require developers to use the same programming language.

    3. C

      They eliminate the need for documentation in software projects.

    4. D

      They enforce specific coding syntax that must be used across all projects.

    Correct answer
    A

    They provide a common meta-language that developers can use to understand each other's code.

    Explanation

    Design patterns serve as a common vocabulary for developers, allowing them to understand each other's designs and implementations more easily.

  3. 3

    In the context of design patterns, what does 'creational' refer to?

    1. A

      Patterns that manage object creation mechanisms in flexible ways.

    2. B

      Patterns that handle object destruction and memory management.

    3. C

      Patterns that improve the structure of object hierarchies.

    4. D

      Patterns that enhance communication between objects.

    Correct answer
    A

    Patterns that manage object creation mechanisms in flexible ways.

    Explanation

    Creational design patterns focus on object creation mechanisms, creating objects in a flexible and adaptable manner.

  4. 4

    Which family of design patterns deals with improving interaction and communication between objects?

    1. A

      Concurrency design patterns.

    2. B

      Creational design patterns.

    3. C

      Structural design patterns.

    4. D

      Behavioral design patterns.

    Correct answer
    D

    Behavioral design patterns.

    Explanation

    Behavioral design patterns improve interaction and communication between objects, providing loosely coupled ways for objects to exchange messages.

  5. 5

    What is the main advantage of using a design pattern as a 'solution template'?

    1. A

      It guarantees the software will be bug-free.

    2. B

      It eliminates the need for any testing.

    3. C

      It automatically generates all the code for the project.

    4. D

      It provides an optimized and reusable approach that has been tested in many projects.

    Correct answer
    D

    It provides an optimized and reusable approach that has been tested in many projects.

    Explanation

    Design patterns are optimized solutions that have been used successfully in many projects, saving time and providing reliability.

  6. 6

    According to the content, which three pattern families are covered in this course?

    1. A

      Structural, behavioral, and concurrent.

    2. B

      Creational, architectural, and structural.

    3. C

      Creational, structural, and behavioral.

    4. D

      Behavioral, concurrency, and creational.

    Correct answer
    C

    Creational, structural, and behavioral.

    Explanation

    The course covers creational, structural, and behavioral design patterns.

  7. 7

    What is the primary goal of software architecture according to the course?

    1. A

      To replace the need for coding altogether.

    2. B

      To create a high-level blueprint that helps coordinate all aspects of a software project.

    3. C

      To ensure the software runs as fast as possible.

    4. D

      To eliminate the need for requirement gathering.

    Correct answer
    B

    To create a high-level blueprint that helps coordinate all aspects of a software project.

    Explanation

    Software architecture provides a high-level blueprint that helps coordinate from requirements to implementation, similar to building a skyscraper.

  8. 8

    What does 'loose coupling' mean in the context of good architecture?

    1. A

      Components are tightly packed together to reduce memory usage.

    2. B

      Weak knowledge association between components, so changes to one component minimally affect others.

    3. C

      Components are completely independent and never communicate.

    4. D

      Strong interdependencies between components for better performance.

    Correct answer
    B

    Weak knowledge association between components, so changes to one component minimally affect others.

    Explanation

    Loose coupling means components have weak knowledge of each other, so changes to one component least affect the existence or performance of others.

  9. 9

    What is the Law of Demeter (LoD) principle?

    1. A

      Every class should be accessible from anywhere in the program.

    2. B

      Only one instance of each class should exist.

    3. C

      All methods should be public to maximize flexibility.

    4. D

      Each unit should only talk to its immediate friends and not to strangers.

    Correct answer
    D

    Each unit should only talk to its immediate friends and not to strangers.

    Explanation

    The Law of Demeter states that a given object should assume as little as possible about the structure of anything else other than its closest friends.

  10. 10

    Which SOLID principle states that classes should be open for extension but closed for modification?

    1. A

      Open/Closed Principle.

    2. B

      Dependency Inversion Principle.

    3. C

      Liskov Substitution Principle.

    4. D

      Single Responsibility Principle.

    Correct answer
    A

    Open/Closed Principle.

    Explanation

    The Open/Closed Principle states that software entities should be open for extension but closed for modification.

  11. 11

    What does the Single Responsibility Principle (SRP) require?

    1. A

      A class should be responsible for all operations related to its data.

    2. B

      A class should inherit from only one parent class.

    3. C

      A class should be as small as possible.

    4. D

      A class should have only one reason to change, meaning one central responsibility.

    Correct answer
    D

    A class should have only one reason to change, meaning one central responsibility.

    Explanation

    SRP states that there should never be more than one reason for a class to change, each class should have only one responsibility.

  12. 12

    According to the Dependency Inversion Principle (DIP), you should depend on:

    1. A

      Abstractions, not concretions.

    2. B

      Low-level modules, not high-level modules.

    3. C

      Concrete classes, not interfaces.

    4. D

      Detailed implementations, not abstractions.

    Correct answer
    A

    Abstractions, not concretions.

    Explanation

    Dependency Inversion Principle says to depend upon abstractions, not concretions.

  13. 13

    What type of UML diagram is used to convey the structure of a design pattern?

    1. A

      Sequence diagram.

    2. B

      Class diagram.

    3. C

      Activity diagram.

    4. D

      Use case diagram.

    Correct answer
    B

    Class diagram.

    Explanation

    Class diagrams are used to convey the structure of a design pattern, showing relationships between classes.

  14. 14

    In UML, what does a white diamond (hollow diamond) represent in a class relationship?

    1. A

      Aggregation, where the part can exist independently from the whole.

    2. B

      Inheritance, where the child inherits from the parent.

    3. C

      Composition, where the part cannot exist without the whole.

    4. D

      Dependency, where one class uses another.

    Correct answer
    A

    Aggregation, where the part can exist independently from the whole.

    Explanation

    A white diamond indicates aggregation, meaning the contained object can exist independently of the container. For example, a pool can exist without the house.

  15. 15

    In UML, which symbol is used to denote a private attribute or method?

    1. A

      A plus sign (+).

    2. B

      A tilde (~).

    3. C

      A hash sign (#).

    4. D

      A minus sign (-).

    Correct answer
    D

    A minus sign (-).

    Explanation

    In UML, a minus sign (-) indicates private visibility, while a plus sign (+) indicates public.

  16. 16

    What is the purpose of a sequence diagram in UML?

    1. A

      To show the static structure of classes and their relationships.

    2. B

      To model the behavior of a system as a finite state machine.

    3. C

      To illustrate the physical deployment of components on hardware.

    4. D

      To show the interaction over time between objects, including message exchange.

    Correct answer
    D

    To show the interaction over time between objects, including message exchange.

    Explanation

    Sequence diagrams are used to visualize object-oriented behavior, showing message exchange over time.

  17. 17

    In a UML class diagram, what does a dashed arrow with an open triangle head represent?

    1. A

      An association relationship.

    2. B

      A generalization (inheritance) relationship.

    3. C

      A realization (interface implementation) relationship.

    4. D

      A dependency relationship.

    Correct answer
    C

    A realization (interface implementation) relationship.

    Explanation

    A dashed arrow with an open triangle indicates realization, such as a class implementing an interface.

  18. 18

    What is the correct way to create an abstract class in Python?

    1. A

      Inherit from the ABC class and use the @abstractmethod decorator.

    2. B

      Define a class with all methods having only pass.

    3. C

      Use the abstract keyword before the class definition.

    4. D

      Inherit from object and use the @abstract decorator.

    Correct answer
    A

    Inherit from the ABC class and use the @abstractmethod decorator.

    Explanation

    In Python, an abstract class is created by inheriting from ABC (Abstract Base Class) and decorating abstract methods with @abstractmethod.

  19. 19

    What distinguishes an abstract method from a regular method in Python?

    1. A

      An abstract method is faster because it is not compiled.

    2. B

      An abstract method has no implementation and must be overridden in derived classes.

    3. C

      An abstract method can only be called within abstract classes.

    4. D

      An abstract method is automatically implemented by the base class.

    Correct answer
    B

    An abstract method has no implementation and must be overridden in derived classes.

    Explanation

    Abstract methods are declared without implementation (using pass) and must be implemented by concrete subclasses.

  20. 20

    What is the purpose of the __init__ method in a Python class?

    1. A

      To define the class-level static variable.

    2. B

      To create a new instance of the class.

    3. C

      To destroy the object when it is no longer needed.

    4. D

      To initialize the object's attributes after it has been created by __new__.

    Correct answer
    D

    To initialize the object's attributes after it has been created by __new__.

    Explanation

    The __init__ method is an instance method that initializes the object's attributes after the object is created by __new__.

  21. 21

    In Python, what technique is used to create a private attribute (with name mangling)?

    1. A

      Prefixing the attribute name with a double underscore (__).

    2. B

      Using the @private decorator.

    3. C

      Prefixing the attribute name with a single underscore (_).

    4. D

      Declaring the attribute as private.

    Correct answer
    A

    Prefixing the attribute name with a double underscore (__).

    Explanation

    A double underscore prefix (__) triggers name mangling in Python, making it harder to accidentally access from outside the class.

  22. 22

    What is encapsulation in the context of object-oriented programming?

    1. A

      Separating data and methods into different files.

    2. B

      Gathering all relevant data and functionality in one place, controlling complexity.

    3. C

      Making all class members public for ease of access.

    4. D

      Using inheritance to share code between classes.

    Correct answer
    B

    Gathering all relevant data and functionality in one place, controlling complexity.

    Explanation

    Encapsulation is the bundling of data and methods that operate on that data into a single unit, controlling access and complexity.

  23. 23

    What is the key purpose of the Singleton design pattern?

    1. A

      To create multiple instances of a class that share data.

    2. B

      To ensure a class has only one instance and provides a global point of access to it.

    3. C

      To provide a way to create objects without specifying their concrete classes.

    4. D

      To separate the construction of a complex object from its representation.

    Correct answer
    B

    To ensure a class has only one instance and provides a global point of access to it.

    Explanation

    Singleton ensures a class has only one instance and provides global access to that instance.

  24. 24

    What is 'lazy construction' in the context of the Singleton pattern?

    1. A

      The Singleton instance is created in a separate thread.

    2. B

      The Singleton instance is created immediately when the program starts.

    3. C

      The Singleton instance is never created and remains null.

    4. D

      The Singleton instance is created only when it is first needed.

    Correct answer
    D

    The Singleton instance is created only when it is first needed.

    Explanation

    Lazy construction (or lazy instantiation) creates the Singleton instance only when it is first accessed, saving resources if it may never be used.

  25. 25

    Which method is responsible for creating and returning a new instance of a class in Python and is often overridden in Singleton implementations?

    1. A

      __init__

    2. B

      __del__

    3. C

      __new__

    4. D

      __call__

    Correct answer
    C

    __new__

    Explanation

    The __new__ method is a static method responsible for creating a new instance. Overriding it allows control over instance creation, useful for Singleton.

  26. 26

    What is the main advantage of using a metaclass for implementing Singleton in Python?

    1. A

      It is the only way to implement Singleton in Python.

    2. B

      It allows controlling class creation itself, providing a clean way to enforce Singleton behavior on multiple classes.

    3. C

      It makes the Singleton instance thread-safe automatically.

    4. D

      It enables eager loading by default.

    Correct answer
    B

    It allows controlling class creation itself, providing a clean way to enforce Singleton behavior on multiple classes.

    Explanation

    A metaclass customizes class creation, allowing the Singleton logic to be defined once and applied to multiple classes, producing cleaner code.

  27. 27

    What is eager loading in the context of a Singleton?

    1. A

      The Singleton instance is created when the class is loaded into memory, before it is ever requested.

    2. B

      The Singleton instance is created multiple times but only the first one is used.

    3. C

      The Singleton instance is created lazily but with a priority queue.

    4. D

      The Singleton instance is created only when a certain method is called.

    Correct answer
    A

    The Singleton instance is created when the class is loaded into memory, before it is ever requested.

    Explanation

    Eager loading creates the Singleton instance at class loading time, so it is ready immediately when first requested.

  28. 28

    When implementing a thread-safe Singleton in Python, what is the purpose of using a lock?

    1. A

      To allow multiple threads to create different instances.

    2. B

      To prevent multiple threads from reading the instance simultaneously.

    3. C

      To increase the speed of instance creation.

    4. D

      To ensure only one thread at a time can execute the critical section where the instance is created.

    Correct answer
    D

    To ensure only one thread at a time can execute the critical section where the instance is created.

    Explanation

    A lock ensures mutual exclusion in the critical section (instance creation), preventing race conditions that could create multiple instances.

  29. 29

    Which of the following is a common use case for the Singleton pattern?

    1. A

      Creating multiple database connections for better performance.

    2. B

      Implementing different rendering strategies for the UI.

    3. C

      Logging, where all parts of the application should write to the same log file.

    4. D

      Allowing different modules to have their own independent configuration.

    Correct answer
    C

    Logging, where all parts of the application should write to the same log file.

    Explanation

    Logging often requires a single point of access to a log file, making Singleton a good choice.

  30. 30

    What is the main architectural disadvantage of using a Constructor directly to create objects (as opposed to a factory method)?

    1. A

      Constructors cannot take parameters.

    2. B

      Constructors are not available in object-oriented languages.

    3. C

      Constructors are slower than factory methods.

    4. D

      The code becomes strongly coupled to the specific class being instantiated.

    Correct answer
    D

    The code becomes strongly coupled to the specific class being instantiated.

    Explanation

    Using a constructor directly couples the code to the exact type, making it hard to introduce new types without modifying existing code.

  31. 31

    What does the Factory Method pattern abstract out?

    1. A

      The business logic of processing data.

    2. B

      The creation logic of specific class instances, so the client does not need to know the concrete class.

    3. C

      The storage and retrieval of data.

    4. D

      The UI rendering logic.

    Correct answer
    B

    The creation logic of specific class instances, so the client does not need to know the concrete class.

    Explanation

    The Factory Method pattern abstracts object creation, allowing the client to work with interfaces rather than concrete implementations.

  32. 32

    In the simple (parameterized) Factory Method variant, how does the factory determine which concrete object to create?

    1. A

      It randomly selects a class from a list.

    2. B

      It prompts the user to choose at runtime.

    3. C

      It uses a parameter (e.g., a string or enum) to decide which class to instantiate.

    4. D

      It always creates the same object type.

    Correct answer
    C

    It uses a parameter (e.g., a string or enum) to decide which class to instantiate.

    Explanation

    The simple factory takes a parameter (like a type identifier) and uses conditional logic to create the appropriate concrete instance.

  33. 33

    What is a key benefit of using the Factory Method pattern with respect to the Open/Closed Principle?

    1. A

      It eliminates the need for interfaces in the code.

    2. B

      It forces all classes to be sealed.

    3. C

      New subtypes can be introduced without breaking existing client code.

    4. D

      It allows clients to modify the factory logic at runtime.

    Correct answer
    C

    New subtypes can be introduced without breaking existing client code.

    Explanation

    The Factory Method pattern allows adding new concrete classes without changing the client code that uses the factory, supporting the Open/Closed Principle.

  34. 34

    In the classic Gang of Four Factory Method variant, what is a concrete Creator responsible for?

    1. A

      Implementing the factory method to create a specific concrete product.

    2. B

      Directly instantiating objects using constructors.

    3. C

      Managing the lifecycle of all created objects.

    4. D

      Defining the interface for all factories.

    Correct answer
    A

    Implementing the factory method to create a specific concrete product.

    Explanation

    In the classic pattern, each concrete Creator implements the factory method to create a particular concrete product.

  35. 35

    What is a potential downside of using the Factory Method pattern?

    1. A

      It forces the use of inheritance, which is always harmful.

    2. B

      It cannot be used with abstract classes.

    3. C

      It can increase the number of classes in the system, as each concrete product may require a concrete creator.

    4. D

      It makes the code less flexible.

    Correct answer
    C

    It can increase the number of classes in the system, as each concrete product may require a concrete creator.

    Explanation

    The classic Factory Method can lead to many small classes, though the parameterized variant reduces this.

  36. 36

    When would you consider using the Factory Method pattern?

    1. A

      When you need to build a complex object step by step.

    2. B

      When you want to reuse the same object instance.

    3. C

      When you need to create a single object that is used globally.

    4. D

      When a class cannot anticipate the type of objects it must create.

    Correct answer
    D

    When a class cannot anticipate the type of objects it must create.

    Explanation

    Factory Method is useful when the client doesn't know in advance the exact class of objects it needs to create.

  37. 37

    What is the core problem that the Builder pattern solves?

    1. A

      The need to create a single instance of a class.

    2. B

      The construction of a complex object is separated from its representation, allowing the same construction process to create different representations.

    3. C

      The incompatibility between two interfaces.

    4. D

      The need to notify multiple objects about state changes.

    Correct answer
    B

    The construction of a complex object is separated from its representation, allowing the same construction process to create different representations.

    Explanation

    The Builder pattern separates the construction of a complex object from its representation, enabling multiple representations using the same steps.

  38. 38

    In the Builder pattern, what role does the Director play?

    1. A

      It controls the construction process by delegating to a Builder and coordinating the steps.

    2. B

      It stores the constructed product.

    3. C

      It defines the interface for building parts.

    4. D

      It directly creates the final product without using a builder.

    Correct answer
    A

    It controls the construction process by delegating to a Builder and coordinating the steps.

    Explanation

    The Director aggregates a Builder and calls its step methods in a specific sequence to construct a product.

  39. 39

    What is a common use case for the Builder pattern?

    1. A

      Creating a single instance of a logger.

    2. B

      Adapting one interface to another.

    3. C

      Building complex objects like documents (e.g., RTF to PDF) or houses with many optional parts.

    4. D

      Notifying multiple observers of changes.

    Correct answer
    C

    Building complex objects like documents (e.g., RTF to PDF) or houses with many optional parts.

    Explanation

    Builder is used for objects with many parts and multiple representations, such as documents or houses.

  40. 40

    Why is using a long constructor with many parameters considered a problem in the context of the Builder pattern?

    1. A

      It becomes ugly, unreadable, and difficult to maintain, especially when many parameters are optional.

    2. B

      It is the recommended way to create all objects.

    3. C

      It makes the object immutable.

    4. D

      It improves performance by reducing method calls.

    Correct answer
    A

    It becomes ugly, unreadable, and difficult to maintain, especially when many parameters are optional.

    Explanation

    A telescoping constructor with many parameters is hard to read and maintain; the Builder pattern provides a cleaner step-by-step alternative.

  41. 41

    In the Builder pattern, what is the relationship between the Builder and the Product?

    1. A

      The Builder constructs and assembles the parts of the Product.

    2. B

      The Builder is the same as the Product.

    3. C

      The Product defines the builder interface.

    4. D

      The Product contains the Builder.

    Correct answer
    A

    The Builder constructs and assembles the parts of the Product.

    Explanation

    The Builder is responsible for constructing the Product by building its parts.

  42. 42

    What design pattern should be used to make the Builder pattern's builder class a singleton?

    1. A

      Strategy pattern.

    2. B

      Adapter pattern.

    3. C

      Singleton pattern.

    4. D

      Factory Method pattern.

    Correct answer
    C

    Singleton pattern.

    Explanation

    Often, the Builder implementation may be used as a Singleton to ensure only one builder instance exists.

  43. 43

    What is the main purpose of the Adapter design pattern?

    1. A

      To define a one-to-many dependency between objects.

    2. B

      To allow an object to change its behavior when its internal state changes.

    3. C

      To convert the interface of a class into another interface that a client expects, allowing incompatible classes to work together.

    4. D

      To create a family of related objects.

    Correct answer
    C

    To convert the interface of a class into another interface that a client expects, allowing incompatible classes to work together.

    Explanation

    The Adapter pattern bridges incompatible interfaces by converting one interface to another that the client expects.

  44. 44

    In the Adapter pattern, what is the 'Adaptee'?

    1. A

      The client that uses the adapted interface.

    2. B

      The new interface that the client expects.

    3. C

      The class that converts the Adaptee to the Target interface.

    4. D

      The existing class that needs to be adapted to work with the client.

    Correct answer
    D

    The existing class that needs to be adapted to work with the client.

    Explanation

    The Adaptee is the existing class with an incompatible interface that needs to be adapted.

  45. 45

    What are the two main types of adaptation that an Adapter can perform?

    1. A

      Data format conversion and interface conversion.

    2. B

      Memory management and thread synchronization.

    3. C

      Algorithm optimization and code refactoring.

    4. D

      UI styling and layout adjustment.

    Correct answer
    A

    Data format conversion and interface conversion.

    Explanation

    Adapters can convert data from one format to another (e.g., XML to JSON) or convert one interface to another (e.g., legacy rectangle to new rectangle).

  46. 46

    Which of the following best describes the relationship between Adapter and composition?

    1. A

      The Adapter creates multiple instances of the Adaptee.

    2. B

      The Adapter inherits from the Adaptee to reuse its code.

    3. C

      The Adapter class holds a reference to the Adaptee and delegates work to it, often using composition.

    4. D

      The Adapter is composed of many different clients.

    Correct answer
    C

    The Adapter class holds a reference to the Adaptee and delegates work to it, often using composition.

    Explanation

    Adapter typically uses composition by wrapping the Adaptee and delegating calls to it after performing necessary conversions.

  47. 47

    When should you consider using the Adapter pattern?

    1. A

      When you need to encapsulate a family of algorithms.

    2. B

      When you want to ensure only one instance of a class exists.

    3. C

      When you have an existing class with an interface that is not compatible with the rest of your code.

    4. D

      When you need to create a new class from scratch.

    Correct answer
    C

    When you have an existing class with an interface that is not compatible with the rest of your code.

    Explanation

    Use Adapter when you want to reuse an existing class but its interface doesn't match what your code expects.

  48. 48

    What is a potential downside of using the Adapter pattern?

    1. A

      It requires rewriting the existing class.

    2. B

      It adds extra layers of indirection, which can increase complexity and potentially affect performance.

    3. C

      It only works with data format conversions.

    4. D

      It makes the code impossible to maintain.

    Correct answer
    B

    It adds extra layers of indirection, which can increase complexity and potentially affect performance.

    Explanation

    Introducing an adapter adds a new class/interface, which may increase complexity and a slight overhead.

  49. 49

    What is the core idea of the Strategy design pattern?

    1. A

      Ensuring a class has only one instance.

    2. B

      Defining a one-to-many dependency between objects.

    3. C

      Defining a family of algorithms, encapsulating each one, and making them interchangeable.

    4. D

      Adapting one interface to another.

    Correct answer
    C

    Defining a family of algorithms, encapsulating each one, and making them interchangeable.

    Explanation

    Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable within a context.

  50. 50

    In the Strategy pattern, what is the Context?

    1. A

      The class that maintains a reference to a Strategy object and can swap strategies at runtime.

    2. B

      The client that uses the strategies directly.

    3. C

      The concrete implementation of an algorithm.

    4. D

      The interface that defines the method for all strategies.

    Correct answer
    A

    The class that maintains a reference to a Strategy object and can swap strategies at runtime.

    Explanation

    The Context is the object that uses a Strategy; it has an aggregation of a Strategy interface and can change it via a setter.

  51. 51

    How does the Strategy pattern follow the Open/Closed Principle?

    1. A

      The pattern forbids adding new strategies once the application is deployed.

    2. B

      Strategies can only be changed at compile time.

    3. C

      The Context class must be modified every time a new strategy is added.

    4. D

      New strategies can be introduced without modifying the Context or client code.

    Correct answer
    D

    New strategies can be introduced without modifying the Context or client code.

    Explanation

    Because strategies are separate classes implementing a common interface, new strategies can be added without changing existing code.

  52. 52

    What is a common use case for the Strategy pattern?

    1. A

      Logging to a single file.

    2. B

      Building a complex object step by step.

    3. C

      Notifying multiple observers about state changes.

    4. D

      Switching between different algorithms (e.g., data extraction methods) at runtime.

    Correct answer
    D

    Switching between different algorithms (e.g., data extraction methods) at runtime.

    Explanation

    Strategy is ideal for scenarios where an object needs to use different algorithms (e.g., different parsing or calculation methods) interchangeably.

  53. 53

    Which design pattern is considered an extension of the Strategy pattern?

    1. A

      State pattern.

    2. B

      Singleton pattern.

    3. C

      Observer pattern.

    4. D

      Adapter pattern.

    Correct answer
    A

    State pattern.

    Explanation

    State pattern is often considered an extension of Strategy because both use composition to delegate behavior, but State allows the object to change behavior when its internal state changes.

  54. 54

    What is a disadvantage of using the Strategy pattern?

    1. A

      Clients must be aware of the differences between strategies to select the right one.

    2. B

      It forces all strategies to be public.

    3. C

      It cannot be used with interfaces.

    4. D

      It makes the code highly coupled.

    Correct answer
    A

    Clients must be aware of the differences between strategies to select the right one.

    Explanation

    Clients need to understand the differences between strategies to choose appropriately, which may add complexity.

  55. 55

    What is the primary intent of the Observer design pattern?

    1. A

      To allow an object to alter its behavior when its internal state changes.

    2. B

      To ensure a class has only one instance.

    3. C

      To convert the interface of a class into another interface.

    4. D

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

    Correct answer
    D

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

    Explanation

    The Observer pattern defines a one-to-many dependency, where a subject notifies all observers of any state changes.

  56. 56

    In the Observer pattern, what are the names for the publisher and subscriber objects?

    1. A

      Director and Builder.

    2. B

      Client and Adapter.

    3. C

      Context and Strategy.

    4. D

      Subject and Observer.

    Correct answer
    D

    Subject and Observer.

    Explanation

    The subject (publisher) maintains a list of observers (subscribers) and notifies them.

  57. 57

    What is a common use case for the Observer pattern?

    1. A

      Building a complex object step by step.

    2. B

      Creating a single point of access to a resource.

    3. C

      Implementing event notification systems, such as model-view-controller (MVC) architectures.

    4. D

      Converting data from one format to another.

    Correct answer
    C

    Implementing event notification systems, such as model-view-controller (MVC) architectures.

    Explanation

    Observer is used for event handling, such as in MVC where the model notifies views of changes.

  58. 58

    What is a potential drawback of the Observer pattern?

    1. A

      It only works with single-threaded applications.

    2. B

      Observers cannot be added or removed dynamically.

    3. C

      The order in which observers are notified can be unpredictable, potentially leading to issues if order matters.

    4. D

      The subject must know the concrete classes of all observers.

    Correct answer
    C

    The order in which observers are notified can be unpredictable, potentially leading to issues if order matters.

    Explanation

    The notification order depends on the data structure used (e.g., list), and may be non-deterministic, which can be problematic if order is important.

  59. 59

    Which pattern is key to reactive behavior in software applications?

    1. A

      Builder pattern.

    2. B

      Singleton pattern.

    3. C

      Adapter pattern.

    4. D

      Observer pattern.

    Correct answer
    D

    Observer pattern.

    Explanation

    Observer enables reactivity: when something happens (event), the system reacts by notifying subscribers.

  60. 60

    In the context of the Observer pattern, what is the role of the update method?

    1. A

      It is called by the subject to notify observers of a state change.

    2. B

      It changes the state of the subject.

    3. C

      It initializes the subject with data.

    4. D

      It removes an observer from the list.

    Correct answer
    A

    It is called by the subject to notify observers of a state change.

    Explanation

    The update method is the notification method that the subject calls on each observer to inform it of changes.

  61. 61

    What is the purpose of the State design pattern?

    1. A

      To allow an object to alter its behavior when its internal state changes, making it appear to change its class.

    2. B

      To adapt an interface to another.

    3. C

      To define a one-to-many dependency between objects.

    4. D

      To create a single instance of a class.

    Correct answer
    A

    To allow an object to alter its behavior when its internal state changes, making it appear to change its class.

    Explanation

    State pattern enables an object to change its behavior when its internal state changes, as if it changed its class.

  62. 62

    In the State pattern, what does the Context object contain?

    1. A

      A reference to a State object that represents the current state of the context.

    2. B

      A list of observers that are notified of changes.

    3. C

      A builder for constructing products.

    4. D

      A factory method for creating objects.

    Correct answer
    A

    A reference to a State object that represents the current state of the context.

    Explanation

    The Context aggregates a State object, and delegates state-specific behavior to that object.

  63. 63

    How does the State pattern relate to the Strategy pattern?

    1. A

      State is an extension of Strategy; both use composition, but State allows the object to change behavior when its internal state changes.

    2. B

      State uses Strategy to select algorithms.

    3. C

      State is the same as Strategy.

    4. D

      State is completely unrelated to Strategy.

    Correct answer
    A

    State is an extension of Strategy; both use composition, but State allows the object to change behavior when its internal state changes.

    Explanation

    Both patterns use composition to delegate behavior, but State changes behavior based on internal state, while Strategy allows interchangeable algorithms.

  64. 64

    What is a common example used to illustrate the State pattern?

    1. A

      A shape factory that creates different shapes.

    2. B

      A traffic light that cycles through states (green, yellow, red) and changes behavior accordingly.

    3. C

      A database connection pool.

    4. D

      A logger that writes to a file.

    Correct answer
    B

    A traffic light that cycles through states (green, yellow, red) and changes behavior accordingly.

    Explanation

    Traffic lights have clear states (red, yellow, green) and transitions, making them a classic state machine example.

  65. 65

    What is a potential con of using the State pattern?

    1. A

      It forces all states to be singletons.

    2. B

      It makes the code less maintainable because states are scattered.

    3. C

      It cannot be used with object-oriented languages.

    4. D

      It can require a lot of code as the number of states grows.

    Correct answer
    D

    It can require a lot of code as the number of states grows.

    Explanation

    As more states are added, the number of state classes increases, potentially leading to code bloat.

  66. 66

    In the State pattern, how are transitions typically handled?

    1. A

      The context handles all transitions using large conditional statements.

    2. B

      The client explicitly sets the next state each time.

    3. C

      Transitions are defined in a separate transition table.

    4. D

      The state objects themselves are aware of the next state and change it by setting the context's state.

    Correct answer
    D

    The state objects themselves are aware of the next state and change it by setting the context's state.

    Explanation

    In the typical pattern, each concrete state knows its successor and sets the context's state when a transition occurs.

  67. 67

    What is the universe of Conway's Game of Life?

    1. A

      An infinite two-dimensional orthogonal grid of square cells, each either alive or dead.

    2. B

      A random collection of points in space.

    3. C

      A three-dimensional toroidal grid.

    4. D

      A finite one-dimensional array of cells.

    Correct answer
    A

    An infinite two-dimensional orthogonal grid of square cells, each either alive or dead.

    Explanation

    The Game of Life is set on an infinite two-dimensional grid of squares, each cell having two states (alive or dead).

  68. 68

    According to Conway's rules, what happens to a live cell with exactly two or three live neighbors?

    1. A

      It dies of loneliness.

    2. B

      It spawns a new cell.

    3. C

      It becomes dead by overpopulation.

    4. D

      It survives to the next generation.

    Correct answer
    D

    It survives to the next generation.

    Explanation

    A live cell with two or three live neighbors lives on; otherwise, it dies.

  69. 69

    What happens to a dead cell with exactly three live neighbors?

    1. A

      It becomes a new cell type.

    2. B

      It dies of overpopulation.

    3. C

      It remains dead.

    4. D

      It becomes a live cell (birth).

    Correct answer
    D

    It becomes a live cell (birth).

    Explanation

    A dead cell with exactly three live neighbors becomes alive, mimicking reproduction.

  70. 70

    What does the term 'tick' refer to in the context of the Game of Life?

    1. A

      The time interval at which the simulation updates to the next generation.

    2. B

      The number of live cells on the grid.

    3. C

      A specific pattern that repeats.

    4. D

      The act of drawing a cell.

    Correct answer
    A

    The time interval at which the simulation updates to the next generation.

    Explanation

    A tick is the unit of time after which the simulation processes the rules and transitions to the next generation.

  71. 71

    What is the recommended approach to start architecting a Game of Life simulation according to the course?

    1. A

      Start by implementing all design patterns simultaneously.

    2. B

      Use only a single class for the entire simulation.

    3. C

      Begin coding the UI immediately without planning.

    4. D

      Write down the workflow, define inputs/outputs, outline main agents, create a proof of concept, and refine iteratively.

    Correct answer
    D

    Write down the workflow, define inputs/outputs, outline main agents, create a proof of concept, and refine iteratively.

    Explanation

    The course suggests a systematic design process: workflow, agents, patterns, proof of concept, then refine.

  72. 72

    Which design pattern is recommended for making the UI renderers pluggable in the Game of Life?

    1. A

      Factory Method pattern.

    2. B

      Singleton pattern.

    3. C

      Observer pattern.

    4. D

      Strategy pattern.

    Correct answer
    D

    Strategy pattern.

    Explanation

    The Strategy pattern allows swapping different renderers for the grid and cells without changing the rest of the code.

  73. 73

    What is the main role of the 'Generator' or 'Timer' in the Game of Life architecture?

    1. A

      To save and load the simulation state.

    2. B

      To produce ticks at regular intervals, triggering the computation of the next generation.

    3. C

      To draw the grid on the screen.

    4. D

      To handle user input for initial state.

    Correct answer
    B

    To produce ticks at regular intervals, triggering the computation of the next generation.

    Explanation

    A timer generates periodic ticks, and the Observer pattern can be used to notify the simulation to compute the next generation.

Question 1 of 73
Question 1 of 73

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent