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

Study methods

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

Design Patterns Introduction

  • 31Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Introduction to Design Patterns

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

31 questions

  1. 1

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

    1. A

      To replace the need for any architectural planning in software projects

    2. B

      To provide ready-made code snippets that can be copied directly into any project

    3. C

      To offer standardized, reusable solutions to common problems that improve code quality and communication

    4. D

      To enforce a specific programming language and framework for all projects

    Correct answer
    C

    To offer standardized, reusable solutions to common problems that improve code quality and communication

    Explanation

    Design patterns provide standard ways to solve recurring problems, ensuring solutions are tested and improve scalability, testability, and separation of concerns.

  2. 2

    Which of the following is NOT a benefit of using design patterns as mentioned in the transcript?

    1. A

      They guarantee that the solution is the most efficient in all situations

    2. B

      They introduce standard terminology for easier communication among developers

    3. C

      They enhance testability and scalability of the application

    4. D

      They promote separation of concerns and better architecture

    Correct answer
    A

    They guarantee that the solution is the most efficient in all situations

    Explanation

    Design patterns offer proven solutions but do not guarantee absolute efficiency; they are best practices, but context matters.

  3. 3

    What is the 'Gang of Four' in the context of design patterns?

    1. A

      A set of four design patterns that are considered the most important

    2. B

      A development team that created the first design pattern compiler

    3. C

      A group of four programming languages that support design patterns natively

    4. D

      Four software architects who wrote a foundational book on design patterns

    Correct answer
    D

    Four software architects who wrote a foundational book on design patterns

    Explanation

    The Gang of Four refers to Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, who authored the seminal book on design patterns.

  4. 4

    Which category of design patterns focuses on how objects are created?

    1. A

      Creational patterns

    2. B

      Architectural patterns

    3. C

      Structural patterns

    4. D

      Behavioral patterns

    Correct answer
    A

    Creational patterns

    Explanation

    Creational patterns deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.

  5. 5

    What is the main concern of structural design patterns?

    1. A

      How to manage the lifecycle of objects in memory

    2. B

      How classes and objects are composed to form larger structures

    3. C

      How objects are created and instantiated

    4. D

      How objects interact and communicate with each other

    Correct answer
    B

    How classes and objects are composed to form larger structures

    Explanation

    Structural patterns explain how to assemble objects and classes into larger structures while keeping them flexible and efficient.

  6. 6

    Which design pattern type focuses on how components interact and communicate with each other?

    1. A

      Behavioral patterns

    2. B

      Concurrency patterns

    3. C

      Creational patterns

    4. D

      Structural patterns

    Correct answer
    A

    Behavioral patterns

    Explanation

    Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects, focusing on communication.

  7. 7

    What is a key disadvantage of the Singleton pattern mentioned in the transcript?

    1. A

      It consumes too much memory because it creates multiple instances

    2. B

      It cannot be used in multi-threaded applications at all

    3. C

      It breaks the single responsibility principle and can cause testing issues

    4. D

      It is too complex to implement in most programming languages

    Correct answer
    C

    It breaks the single responsibility principle and can cause testing issues

    Explanation

    Singletons violate the single responsibility principle by managing their own lifecycle, and they make testing harder due to tight coupling.

  8. 8

    In the Singleton pattern, how does the naive implementation fail in a multi-threaded environment?

    1. A

      It only works in Python and fails in other languages like Java

    2. B

      It throws an exception if two threads try to access the Singleton at the same time

    3. C

      It causes a deadlock because multiple threads wait for each other

    4. D

      It may create multiple instances when two threads simultaneously check for existence

    Correct answer
    D

    It may create multiple instances when two threads simultaneously check for existence

    Explanation

    The naive implementation without locking can lead to race conditions where two threads both pass the 'if not instance' check and create separate instances.

  9. 9

    What is the primary purpose of the Factory Method pattern?

    1. A

      To provide a simplified interface to a complex subsystem

    2. B

      To copy existing objects without depending on their classes

    3. C

      To create a single instance of a class globally

    4. D

      To separate the creation of an object from its use, allowing dynamic switching of implementations

    Correct answer
    D

    To separate the creation of an object from its use, allowing dynamic switching of implementations

    Explanation

    Factory Method defines an interface for creating an object, but lets subclasses decide which class to instantiate, promoting loose coupling.

  10. 10

    How does Abstract Factory differ from Factory Method?

    1. A

      Abstract Factory is used only for creating database connections, while Factory Method is for any object

    2. B

      Abstract Factory creates a single object, while Factory Method creates multiple objects

    3. C

      Abstract Factory is a factory that creates other factories, adding another level of abstraction

    4. D

      Abstract Factory cannot be implemented in Python due to lack of interfaces

    Correct answer
    C

    Abstract Factory is a factory that creates other factories, adding another level of abstraction

    Explanation

    Abstract Factory provides an interface for creating families of related objects without specifying their concrete classes, essentially a factory of factories.

  11. 11

    What problem does the Builder pattern solve?

    1. A

      It simplifies the construction of an object with many optional parameters, avoiding telescoping constructors

    2. B

      It ensures that only one thread can create an object at a time

    3. C

      It enables copying of complex objects without coupling to their classes

    4. D

      It allows creating a single instance of a resource-intensive object

    Correct answer
    A

    It simplifies the construction of an object with many optional parameters, avoiding telescoping constructors

    Explanation

    The Builder pattern separates the construction of a complex object from its representation, making it easier to handle multiple optional parameters.

  12. 12

    In the transcript, what is the simpler alternative to the classic Builder pattern in Python?

    1. A

      Using a Singleton metaclass to manage instances

    2. B

      Using named parameters with default values in the constructor

    3. C

      Using the Prototype pattern to clone objects

    4. D

      Using the __new__ method with default parameters

    Correct answer
    B

    Using named parameters with default values in the constructor

    Explanation

    Python allows using named parameters with default values in __init__, which can eliminate the need for a separate Builder class for many cases.

  13. 13

    What is the core idea of the Prototype pattern?

    1. A

      To define a family of interchangeable object factories

    2. B

      To create new objects by copying an existing object, known as the prototype

    3. C

      To restrict a class to only one instance

    4. D

      To provide a way to build complex objects step by step

    Correct answer
    B

    To create new objects by copying an existing object, known as the prototype

    Explanation

    The Prototype pattern creates new objects by copying an existing object, avoiding the cost of creating from scratch and hiding the complexity of object creation.

  14. 14

    Which Python module provides built-in support for the Prototype pattern?

    1. A

      collections module

    2. B

      copy module

    3. C

      pickle module

    4. D

      abc module

    Correct answer
    B

    copy module

    Explanation

    Python's copy module provides copy() and deepcopy() functions to implement prototype-based cloning.

  15. 15

    What is the purpose of the Adapter pattern?

    1. A

      To convert the interface of a class into another interface that clients expect

    2. B

      To separate abstraction from implementation so they can vary independently

    3. C

      To provide a simplified interface to a complex system

    4. D

      To attach additional behavior to an object without changing its interface

    Correct answer
    A

    To convert the interface of a class into another interface that clients expect

    Explanation

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

  16. 16

    What problem does the Bridge pattern solve?

    1. A

      It manages the lifecycle of a resource that is expensive to create

    2. B

      It provides a way to access elements of a collection sequentially

    3. C

      It allows an object to alter its behavior when its internal state changes

    4. D

      It avoids class explosion when dealing with multiple orthogonal dimensions (like shape and color)

    Correct answer
    D

    It avoids class explosion when dealing with multiple orthogonal dimensions (like shape and color)

    Explanation

    The Bridge pattern decouples an abstraction from its implementation so that the two can vary independently, preventing an exponential growth of classes.

  17. 17

    In the Bridge pattern example from the transcript, what example is used to illustrate the pattern?

    1. A

      A graphic editor with shapes and colors

    2. B

      A remote control and devices (radio and TV)

    3. C

      A data storage system with caching

    4. D

      A computer with CPU and memory

    Correct answer
    B

    A remote control and devices (radio and TV)

    Explanation

    The transcript uses a remote control (abstraction) and devices (implementation) to demonstrate how the Bridge pattern works.

  18. 18

    What is the main advantage of the Composite pattern?

    1. A

      It provides a way to update the behavior of a class without modifying it

    2. B

      It reduces the number of classes needed in a tree structure

    3. C

      It allows treating individual objects and compositions of objects uniformly

    4. D

      It simplifies the creation of complex objects by separating construction from representation

    Correct answer
    C

    It allows treating individual objects and compositions of objects uniformly

    Explanation

    The Composite pattern enables clients to treat individual objects and composite objects uniformly, making it easier to work with tree structures.

  19. 19

    How does the Decorator pattern add behavior to an object?

    1. A

      By creating a subclass that overrides all methods of the original class

    2. B

      By wrapping the object with a new class that provides additional functionality before or after calling the original method

    3. C

      By modifying the object's class at runtime using reflection

    4. D

      By using a separate factory that generates extended objects

    Correct answer
    B

    By wrapping the object with a new class that provides additional functionality before or after calling the original method

    Explanation

    The Decorator pattern attaches additional responsibilities to an object dynamically by placing it inside a wrapper object that contains the behavior.

  20. 20

    What is the key difference between the Proxy pattern and the Decorator pattern?

    1. A

      Proxy is used for remote objects, while Decorator is for local objects only

    2. B

      Proxy manages the lifecycle of the underlying object, while Decorator simply adds behavior

    3. C

      Proxy provides a simplified interface, while Decorator provides the same interface

    4. D

      Proxy changes the interface of an object, while Decorator keeps the same interface

    Correct answer
    B

    Proxy manages the lifecycle of the underlying object, while Decorator simply adds behavior

    Explanation

    The Proxy pattern controls access and manages the lifecycle of the real subject, whereas the Decorator adds or overrides behavior without managing lifecycle.

  21. 21

    What does the Facade pattern provide?

    1. A

      A method to copy objects without revealing their internal structure

    2. B

      A mechanism to handle multiple requests in a chain

    3. C

      A way to encapsulate a request as an object

    4. D

      A simplified interface to a complex subsystem

    Correct answer
    D

    A simplified interface to a complex subsystem

    Explanation

    The Facade pattern provides a unified and simpler interface to a set of interfaces in a subsystem, making it easier to use.

  22. 22

    What is the primary goal of the Flyweight pattern?

    1. A

      To decouple an abstraction from its implementation

    2. B

      To provide a global point of access to a resource

    3. C

      To reduce memory usage by sharing common parts of objects among multiple objects

    4. D

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

    Correct answer
    C

    To reduce memory usage by sharing common parts of objects among multiple objects

    Explanation

    Flyweight minimizes memory usage by sharing as much data as possible (intrinsic state) with other similar objects.

  23. 23

    In the Flyweight example in the transcript, what is stored in the factory versus what is unique per object?

    1. A

      The factory stores the rank (private, sergeant, major), while the position is unique per fighter

    2. B

      The factory stores the position, while the rank is unique per fighter

    3. C

      The factory stores nothing; each fighter has its own copy of everything

    4. D

      The factory stores the sprite images, while the rank is unique

    Correct answer
    A

    The factory stores the rank (private, sergeant, major), while the position is unique per fighter

    Explanation

    The fighter factory stores only three fighter objects based on rank (intrinsic), while each fighter created by the army has its own state (extrinsic) not shown in the code.

  24. 24

    What is the Chain of Responsibility pattern's basic structure?

    1. A

      A tree structure where leaves and composites are treated uniformly

    2. B

      A set of handlers arranged in a chain where each handler can process a request or pass it to the next

    3. C

      A stack of commands that can be undone and redone

    4. D

      A central mediator that routes messages between components

    Correct answer
    B

    A set of handlers arranged in a chain where each handler can process a request or pass it to the next

    Explanation

    Chain of Responsibility decouples sender and receiver by allowing multiple objects to handle a request, with each handler having a reference to the next.

  25. 25

    What does the Command pattern encapsulate?

    1. A

      A request as an object, allowing parameterization and queuing of requests

    2. B

      The communication between objects via a central mediator

    3. C

      The state of an object to allow undo/redo functionality

    4. D

      The behavior of an object based on its internal state

    Correct answer
    A

    A request as an object, allowing parameterization and queuing of requests

    Explanation

    The Command pattern turns a request into a stand-alone object that contains all information about the request, enabling operations like queuing, logging, and undo.

  26. 26

    In the Interpreter pattern, what are terminal and non-terminal expressions?

    1. A

      Terminal expressions are leaf nodes (e.g., numbers), non-terminal expressions are composite nodes (e.g., operations)

    2. B

      Terminal expressions represent grammar rules, non-terminal represent values

    3. C

      Terminal expressions are the final results, non-terminal are intermediate states

    4. D

      Terminal expressions cannot be changed, non-terminal can be overridden

    Correct answer
    A

    Terminal expressions are leaf nodes (e.g., numbers), non-terminal expressions are composite nodes (e.g., operations)

    Explanation

    In the Interpreter pattern, terminal expressions define the basic elements (like numbers), and non-terminal expressions define the composition rules (like operations).

  27. 27

    What are the two core methods of an Iterator?

    1. A

      execute() and undo()

    2. B

      add() and remove()

    3. C

      next() and hasNext()

    4. D

      push() and pop()

    Correct answer
    C

    next() and hasNext()

    Explanation

    The Iterator pattern typically provides hasNext() to check if more elements exist and next() to retrieve the next element.

  28. 28

    What is the role of the Mediator pattern?

    1. A

      To provide a way to save and restore previous states of an object

    2. B

      To centralize communication between objects, reducing direct dependencies

    3. C

      To define a one-to-many dependency between objects so that when one object changes state, all dependents are notified

    4. D

      To encapsulate a request as an object

    Correct answer
    B

    To centralize communication between objects, reducing direct dependencies

    Explanation

    The Mediator pattern defines an object that encapsulates how a set of objects interact, promoting loose coupling by preventing direct references.

  29. 29

    In the Memento pattern, which component is responsible for storing and restoring the state?

    1. A

      Caretaker

    2. B

      Originator

    3. C

      Memento

    4. D

      Observer

    Correct answer
    A

    Caretaker

    Explanation

    The Caretaker manages the saved states (mementos) and decides when to save and restore, while the Originator creates and restores state using Memento objects.

  30. 30

    What is the main advantage of the Observer pattern over polling?

    1. A

      It eliminates the need for multiple subscribers

    2. B

      It allows synchronous execution of event handlers

    3. C

      It reduces unnecessary communication by notifying only interested parties when an event occurs

    4. D

      It guarantees that events are processed in order

    Correct answer
    C

    It reduces unnecessary communication by notifying only interested parties when an event occurs

    Explanation

    Observer pattern uses a subscribe/notify mechanism where subjects push events to subscribers, avoiding wasteful polling.

  31. 31

    What is a state machine in the context of the State pattern?

    1. A

      A data structure for storing the history of changes

    2. B

      A mechanism to measure the performance of an application

    3. C

      A way to iterate over a collection of objects

    4. D

      An object that can only be in a finite number of predefined states, and its behavior changes based on the current state

    Correct answer
    D

    An object that can only be in a finite number of predefined states, and its behavior changes based on the current state

    Explanation

    The State pattern allows an object to alter its behavior when its internal state changes, appearing as if it changed its class.

Question 1 of 31
Question 1 of 31

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent