# Design Patterns Quiz

## Question 1

When would you use the Factory pattern?

A) When you need exactly one instance
B) When object creation depends on configuration or runtime type
C) When you need to observe changes
D) When you want to add behavior to an object

<!-- ANSWER: B -->
<!-- EXPLANATION: Factory centralizes creation logic. Use it when you need to create different concrete types based on config, type string, or other runtime input, without exposing the creation details to callers. -->

## Question 2

What is the main benefit of the Observer pattern?

A) Better performance
B) Loose coupling between publisher and subscribers
C) Single instance guarantee
D) Easier debugging

<!-- ANSWER: B -->
<!-- EXPLANATION: Observer decouples the subject (publisher) from its observers (subscribers). The subject doesn't need to know who is listening. Subscribers can be added/removed at runtime. -->

## Question 3

The Strategy pattern is useful when:

A) You need multiple instances of the same class
B) You want to switch algorithms at runtime without changing the client
C) You need to wrap an object with extra behavior
D) You have an interface mismatch

<!-- ANSWER: B -->
<!-- EXPLANATION: Strategy extracts algorithms into interchangeable objects. The client holds a strategy and delegates to it. You can swap strategies at runtime (e.g., different sort algorithms, payment methods). -->

## Question 4

How does the Decorator pattern add behavior?

A) By subclassing
B) By wrapping an object and delegating to it
C) By replacing the object
D) By using global state

<!-- ANSWER: B -->
<!-- EXPLANATION: Decorator wraps the original object, adds its own behavior, and delegates to the wrapped object. Multiple decorators can stack (DecoratorA(DecoratorB(Core))). -->

## Question 5

When do you use the Adapter pattern?

A) When you want to optimize performance
B) When an existing class has the wrong interface for your needs
C) When you need a single instance
D) When you want to observe changes

<!-- ANSWER: B -->
<!-- EXPLANATION: Adapter translates between your expected interface and the existing class's interface. Use it when integrating legacy code, third-party libraries, or mismatched APIs. -->

## Question 6

What is a downside of the Singleton pattern?

A) It's hard to implement
B) It can make testing difficult due to global state
C) It only works in single-threaded environments
D) It violates encapsulation

<!-- ANSWER: B -->
<!-- EXPLANATION: Singleton creates global state. In tests, you often want fresh or mock instances. With Singleton, you may need reset logic or dependency injection of the instance to test effectively. -->

## Question 7

<!-- VISUAL: matching -->

Match each design pattern to its typical use case:

A) Factory → 1) Integrating a third-party API with a different interface
B) Observer → 2) Creating different UI components based on OS (Windows/Mac)
C) Adapter → 3) Event system where subscribers react to publisher changes
D) Strategy → 4) Swapping sort algorithms (quick vs merge) at runtime

<!-- ANSWER: A2,B3,C1,D4 -->
<!-- EXPLANATION: Factory creates objects based on config/type (2). Observer decouples publisher from subscribers (3). Adapter translates interfaces (1). Strategy swaps algorithms (4). -->

## Question 8

<!-- VISUAL: matching -->

Match each pattern to its structural characteristic:

A) Decorator → 1) Wraps object, delegates to inner object, adds behavior
B) Singleton → 2) Single instance, global access
C) Strategy → 3) Context holds algorithm object, delegates to it
D) Observer → 4) Subject maintains list of observers, notifies on change

<!-- ANSWER: A1,B2,C3,D4 -->
<!-- EXPLANATION: Decorator wraps and delegates (1). Singleton is single-instance (2). Strategy holds and delegates to strategy object (3). Observer has subject-observer list (4). -->
