Courseiva

CCNA Design Patterns Questions

31 questions · Design Patterns topic · All types, answers revealed

1
MCQeasy

What is the primary benefit of the Facade pattern when working with legacy libraries?

A.It increases the performance of the underlying legacy library
B.It automatically patches security vulnerabilities in legacy code
C.It allows you to rewrite the legacy code
D.It provides a simplified, unified interface to a complex subsystem
AnswerD

This is the definition of the Facade pattern.

Why this answer

A Facade simplifies complex library interactions by providing a cleaner, more intuitive interface for the client.

2
MCQmedium

When implementing the Command pattern in Python, what is a primary advantage of using a 'Command' class hierarchy?

A.It eliminates the need for functions
B.It allows commands to be queued, logged, or undone by the invoker
C.It automatically makes all operations thread-safe
D.It forces the application to use a specific GUI framework
AnswerB

Encapsulation as an object allows for these operations.

Why this answer

It allows commands to be treated as objects, which enables features like queuing, logging, and undo/redo.

3
MCQmedium

In the Decorator pattern, why is it preferred to use the functools.wraps decorator when creating a function decorator?

A.It automatically implements the Singleton pattern
B.It prevents the decorator from being applied multiple times
C.It preserves the metadata of the original function
D.It ensures the function runs faster
AnswerC

This is exactly what functools.wraps is designed for.

Why this answer

functools.wraps preserves the metadata (like __name__ and __doc__) of the original function, which is critical for debugging and reflection.

4
Multi-Selectmedium

Which THREE of the following are common characteristics of the Observer pattern?

Select 3 answers
A.Observers are forced to be synchronous
B.Automatic notification when the subject's state changes
C.A one-to-many relationship between objects
D.The subject is tightly coupled to the concrete observers
E.Loose coupling between the subject and the observers
AnswersB, C, E

This is the primary behavior of the pattern.

Why this answer

One-to-many relationship, event notification, and loose coupling are key.

5
MCQeasy

Which of the following is a classic use case for the Builder pattern in Python?

A.Adding new behaviors to an object at runtime
B.Constructing complex objects step-by-step with various configurations
C.Creating a simple object with no parameters
D.Ensuring only one instance of a class exists
AnswerB

This is the primary purpose of the Builder pattern.

Why this answer

The Builder pattern is ideal when an object requires many steps to construct or has many optional configuration parameters, avoiding 'telescoping constructors'.

6
Multi-Selecteasy

Which TWO of the following are structural patterns?

Select 2 answers
A.Facade
B.Command
C.Observer
D.Builder
E.Adapter
AnswersA, E

Facade is a structural pattern.

Why this answer

Adapter and Facade are classic structural patterns.

7
MCQhard

When implementing the Bridge pattern, what is the primary structural requirement to effectively decouple an abstraction from its implementation?

A.The implementation must use a Singleton to manage its state
B.Both the abstraction and the implementation must inherit from the same class
C.The implementation must be a subclass of the abstraction
D.The abstraction must contain an instance of the implementation interface
AnswerD

This composition allows the implementation to be swapped at runtime.

Why this answer

The abstraction must contain a reference (bridge) to an object of an implementor interface, allowing the implementation to vary independently.

8
MCQhard

You are applying the Facade pattern to simplify a complex subsystem consisting of three interacting legacy modules. What is the primary architectural requirement for the Facade class to be effective?

A.The Facade must inherit from every class in the subsystem
B.The Facade must implement the Singleton pattern to ensure only one interface exists
C.The Facade should expose all methods of the subsystem classes for flexibility
D.The Facade should provide a simplified interface that delegates requests to appropriate subsystem objects
AnswerD

Delegation is the key mechanism of the Facade pattern to hide complexity.

Why this answer

The Facade must act as a single entry point, encapsulating the complexity of the internal modules so the client doesn't need to know about them.

9
MCQeasy

Which pattern is most appropriate for a scenario where you want to provide a standard interface for a suite of related or dependent objects without specifying their concrete classes?

A.Proxy
B.Abstract Factory
C.Singleton
D.Builder
AnswerB

Abstract Factory is specifically designed for families of related objects.

Why this answer

The Abstract Factory pattern provides an interface for creating families of related objects.

10
Multi-Selectmedium

Which THREE of the following are benefits of the Factory Method pattern?

Select 3 answers
A.Adherence to the Open/Closed Principle
B.Eliminating all usage of 'if/else' blocks
C.Decoupling the client code from concrete classes
D.Centralizing object creation logic
E.Automatically making objects immutable
AnswersA, C, D

New product types can be added by creating new creators without changing existing code.

Why this answer

Decoupling, Open/Closed adherence, and centralized logic are the main benefits.

11
Multi-Selectmedium

Which THREE of the following are standard ways to implement the 'Strategy' pattern in Python?

Select 3 answers
A.Injecting the strategy instance at runtime
B.Defining classes with a common interface
C.Hardcoding the strategy selection logic in the context
D.Using a global variable to change strategy behavior
E.Passing a function as an argument
AnswersA, B, E

Dependency injection is standard for the Strategy pattern.

Why this answer

Using functions, classes, or dependency injection are all valid strategies.

12
MCQmedium

In the Memento pattern, which component is responsible for storing the state of the Originator without exposing its internal structure?

A.The Memento
B.The Caretaker
C.The Originator's public properties
D.A global database
AnswerA

The Memento object stores the state effectively acting as a snapshot.

Why this answer

The Memento object acts as an opaque carrier of the state, ensuring the Originator's internals remain private.

13
MCQmedium

In the Observer pattern, what is the best way to prevent memory leaks when the Subject maintains a list of Observers?

A.Use a global list of observers to manage them centrally
B.Set the Observer list to None after every notification cycle
C.Use the weakref module to store references to the Observers
D.Explicitly call the detach method for every Observer
AnswerC

weakref.ref allows the Subject to notify observers without preventing their garbage collection.

Why this answer

If the Subject maintains strong references to Observers, they cannot be garbage collected. Using weak references allows them to be collected when no longer used elsewhere.

14
MCQhard

You are utilizing the Mediator pattern to reduce coupling between components. Which of the following describes the role of the Mediator?

A.It allows components to communicate directly with each other
B.It enforces that components are implemented as Singletons
C.It acts as a central hub that coordinates interactions between components
D.It is responsible for data storage for all components
AnswerC

This is the primary duty of the mediator.

Why this answer

The Mediator centralizes communication between components, so they don't need to refer to each other directly.

15
MCQmedium

You are implementing the Command pattern. How can you best support the 'Undo' operation for a command that changes a document's state?

A.Store the delta or previous state information within the concrete command instance
B.Use the Python 'copy' module to clone the document before every command
C.Store the entire document object in the command history
D.Re-execute the entire command history up to the point of the undo
AnswerA

Storing enough information to reverse the operation is the standard way to implement Undo in the Command pattern.

Why this answer

The Command object must store the previous state or the inverse operation before performing the change, allowing it to revert the state later.

16
MCQmedium

You are implementing a Proxy pattern to provide lazy initialization for a resource-heavy object. Which technique is most effective for checking if the real object exists without triggering its constructor prematurely?

A.Use the @property decorator to trigger instantiation immediately upon object creation
B.Overload the __del__ method to release the resource
C.Use a try-except block around the Proxy's constructor
D.Check if the object attribute is None before calling the constructor
AnswerD

Lazy initialization typically uses a check against None to instantiate the heavy object only when accessed.

Why this answer

The Proxy should hold a reference to the real object and only instantiate it when a method is called that requires the real object's functionality.

17
MCQhard

You are implementing the Visitor pattern to add operations to a set of stable class structures. Why is the 'Double Dispatch' mechanism essential?

A.It prevents the element from needing to know about the visitor
B.It allows the visitor to access private methods of the element
C.It resolves the method call based on the runtime types of both the visitor and the element
D.It allows the visitor to be defined as a singleton
AnswerC

This is the core concept of double dispatch in the Visitor pattern.

Why this answer

Double dispatch ensures that the correct method is executed based on both the type of the Visitor and the type of the Element being visited.

18
Multi-Selecthard

Which TWO of the following are valid ways to implement the Proxy pattern in Python?

Select 2 answers
A.Using the 'import' hook in the sys module
B.Inheriting from the target class and overriding its methods
C.Using a decorator to modify the class structure
D.Changing the global variable scope
E.Using the __getattr__ magic method to intercept property access
AnswersB, E

This is a standard way to implement a virtual or remote proxy.

Why this answer

Proxy can be implemented using class-based wrapping or by overriding magic methods like __getattr__.

19
MCQmedium

You are designing a State pattern. Which component is responsible for changing the state of the context object?

A.The abstract state base class
B.A separate controller object
C.The client code
D.The concrete state objects
AnswerD

Concrete states often define the next state transition, keeping the context clean.

Why this answer

Either the Context or the State objects themselves can transition the state, but encapsulating the transition logic inside the concrete state objects is typical for complex state flows.

20
MCQeasy

Which Python design principle is most strongly supported by the Adapter pattern?

A.Single Responsibility Principle
B.Dependency Inversion Principle
C.Open/Closed Principle
D.Liskov Substitution Principle
AnswerC

It allows adding functionality (via new adapters) without modifying existing client code.

Why this answer

The Adapter pattern allows incompatible interfaces to work together, promoting the Open/Closed Principle by allowing new adapters without changing existing code.

21
Multi-Selectmedium

Which TWO of the following are components of the Model-View-Controller (MVC) pattern frequently seen in Python web frameworks?

Select 2 answers
A.Strategy
B.Model
C.Facade
D.Observer
E.View
AnswersB, E

The Model handles data logic.

Why this answer

The Model (data) and View (representation) are core, while Controllers or View-models mediate.

22
MCQmedium

When implementing the Strategy pattern, what is the best way to inject the strategy into the context object?

A.Pass the strategy instance to the context's constructor
B.Use a global variable for the strategy
C.Import the strategy module inside the context's run method
D.Hardcode the strategy class inside the context's __init__ method
AnswerA

Constructor injection is a standard way to provide the required strategy implementation.

Why this answer

Dependency injection, usually via the constructor, allows the context to remain agnostic of the specific strategy implementation.

23
MCQmedium

You are implementing a Prototype pattern. What is the difference between shallow copy and deep copy in the context of cloning objects?

A.Shallow copy duplicates the reference, deep copy duplicates the object contents
B.Both copies result in the same memory address
C.Shallow copy clones nested objects, deep copy does not
D.Deep copy is always faster than shallow copy
AnswerA

This accurately describes the distinction in Python's copy module.

Why this answer

A shallow copy creates a new object but references the same nested objects, while a deep copy recursively clones all nested objects.

24
MCQhard

You are using the Composite pattern to represent a tree structure of UI elements. What is the most critical challenge when implementing a 'get_parent' method in this pattern?

A.It requires the Leaf objects to also implement the same parent reference logic
B.It makes the children too independent
C.It requires the base Component class to store a reference to the parent
D.Python's memory management cannot handle circular references
AnswerC

To support get_parent, the child needs a reference to its container, which adds maintenance overhead.

Why this answer

The Composite pattern typically flows downward; maintaining an upward reference (parent pointer) creates bidirectional dependencies that make removing or moving nodes complex.

25
MCQmedium

In the Chain of Responsibility pattern, how does a handler decide whether to pass a request to the next handler?

A.By querying the client for the next handler
B.By calling the next handler's handle method only if it cannot fulfill the request
C.By using an exception to jump to the next handler
D.By returning True from a base handler method
AnswerB

The handler checks its own criteria and delegates if necessary.

Why this answer

Each handler evaluates if it can process the request; if not, it calls the successor in the chain.

26
Multi-Selectmedium

Which THREE design patterns are considered 'Behavioral' patterns?

Select 3 answers
A.Strategy
B.Proxy
C.Command
D.Observer
E.Singleton
AnswersA, C, D

Strategy is a Behavioral pattern.

Why this answer

Observer, Command, and Strategy are all classic Behavioral patterns.

27
Multi-Selectmedium

Which TWO of the following are true about the Template Method pattern?

Select 2 answers
A.The base class defines the skeleton of an algorithm
B.It uses inheritance to vary parts of an algorithm
C.It forces the use of multiple inheritance
D.Subclasses are not allowed to override any methods
E.It is a Creational pattern
AnswersA, B

This is the definition of the Template Method.

Why this answer

It provides a skeleton algorithm and allows subclasses to override steps.

28
Multi-Selecthard

Which THREE of the following are essential properties of a robust State pattern implementation?

Select 3 answers
A.The State interface should be as simple as possible
B.The context should store the current state as a private member
C.Each concrete state must be aware of the context to trigger transitions
D.Every state should be a separate Singleton
E.State transitions must be clearly defined
AnswersB, C, E

The context maintains the current state object internally.

Why this answer

State transitions, clear interface, and context awareness are essential.

29
MCQmedium

You are implementing a Singleton pattern in Python using a metaclass. Which mechanism ensures that the __call__ method of the metaclass is only executed once for a specific class?

A.Setting the class variable __instance__ to None in the global scope
B.Implementing a dictionary within the metaclass to cache and return existing instances
C.Overriding the __init__ method of the object class
D.Using the __new__ method of the target class instead of the metaclass
AnswerB

Caching instances in the metaclass's scope allows the __call__ logic to intercept creation and return the singleton.

Why this answer

The metaclass __call__ method controls instance creation. By using a dictionary within the metaclass to store existing instances, you ensure that subsequent calls return the cached instance.

30
MCQhard

You are implementing the Flyweight pattern to optimize memory usage for a text processor. What is the distinction between 'intrinsic' and 'extrinsic' state?

A.Both are shared, but intrinsic is stored in a database
B.Intrinsic state is shared/immutable, extrinsic is context-dependent
C.Neither state can be changed once the Flyweight is instantiated
D.Intrinsic state is the unique data, extrinsic is the shared data
AnswerB

This correctly identifies that intrinsic state is shared and extrinsic is passed in.

Why this answer

Intrinsic state is shared and immutable (stored in the Flyweight), while extrinsic state is unique to the context and passed into the Flyweight's methods.

31
Multi-Selectmedium

Which TWO of the following are common risks when implementing the Singleton pattern in Python?

Select 2 answers
A.Incompatibility with the abc module
B.Excessive subclassing of the Singleton class
C.Race conditions in multi-threaded environments during instantiation
D.Hidden global state dependencies throughout the codebase
E.Increased memory consumption for every instance
AnswersC, D

Without locking, two threads could create two instances simultaneously.

Why this answer

Global state and threading issues are the most prominent risks when implementing the Singleton pattern.

Ready to test yourself?

Try a timed practice session using only Design Patterns questions.