C# delves into Advanced Design Patterns, exploring key patterns like Singleton, Factory, Observer, Strategy, and Dependency Injection. These patterns enhance flexibility, maintainability, and scalability, enabling developers to tackle complex software design challenges more effectively.
MCQs on Advanced Design Patterns in C#
Section 1: Singleton Pattern (6 Questions)
What is the main purpose of the Singleton Pattern?
a) To allow multiple instances of a class
b) To ensure a class has only one instance and provide a global point of access
c) To provide a flexible and reusable object
d) To simplify object creation
Which of the following is a characteristic of the Singleton Pattern?
a) Instantiation is handled externally
b) Only one instance exists in the system
c) The class can have multiple constructors
d) Objects are created lazily
How does the Singleton pattern control instance creation?
a) Using a static constructor
b) Using a private constructor
c) Using a public constructor
d) By creating instances dynamically
In a thread-safe Singleton implementation, which keyword is typically used to prevent multiple threads from creating an instance?
a) volatile
b) lock
c) static
d) private
When implementing the Singleton pattern, which of the following ensures only one instance of a class is created?
a) A static field
b) A static method
c) A public constructor
d) A private field
Which of the following is a disadvantage of the Singleton pattern?
a) High performance
b) Global state can lead to tight coupling
c) Easy to implement
d) Reusable across multiple instances
Section 2: Factory Pattern (6 Questions)
What is the main objective of the Factory Pattern?
a) To create objects without specifying the exact class of object that will be created
b) To create a single instance of an object
c) To store multiple instances of objects
d) To manage dependencies between objects
In the Factory pattern, which method is typically used to create an object?
a) Static constructor
b) Factory method
c) Singleton method
d) Lazy method
What type of object does the Factory Method pattern typically return?
a) Concrete object
b) Abstract object
c) Interface or abstract class
d) Dynamic object
Which of the following is a benefit of using the Factory Pattern?
a) Reduced complexity in code
b) Easy to modify object creation logic
c) Requires less memory
d) Enhances performance
Which pattern is often used in conjunction with the Factory pattern to decouple class instantiation from the client?
a) Dependency Injection
b) Strategy Pattern
c) Observer Pattern
d) Singleton Pattern
What is typically passed to the Factory method to specify the desired object in the Factory Pattern?
a) A string representing object type
b) A configuration object
c) A class name
d) A parameter or configuration
Section 3: Observer Pattern (6 Questions)
What is the primary purpose of the Observer Pattern?
a) To update all dependent objects when a subject changes
b) To create an object only when needed
c) To allow direct access to objects
d) To reduce object dependencies
In the Observer Pattern, what is the role of the subject?
a) To monitor changes in observer states
b) To maintain a list of observers
c) To update the observers when a change occurs
d) To notify the observers directly
How do observers typically receive updates in the Observer Pattern?
a) By polling the subject
b) By subscribing to the subject’s events
c) By directly calling the subject
d) By querying for changes manually
Which of the following can be a problem when using the Observer Pattern?
a) Overloading the observers with too many notifications
b) It is difficult to implement
c) It requires static objects
d) It only works with interfaces
What type of relationship exists between the subject and observer in the Observer Pattern?
a) One-to-many
b) Many-to-many
c) One-to-one
d) Many-to-one
In C#, which event-based mechanism is commonly used to implement the Observer pattern?
a) Delegates and events
b) Interfaces and abstract classes
c) Reflection
d) Threads and locks
Section 4: Strategy Pattern (6 Questions)
What is the main purpose of the Strategy Pattern?
a) To allow different algorithms to be selected at runtime
b) To ensure a class has only one instance
c) To provide a consistent interface for different implementations
d) To manage object dependencies
In the Strategy Pattern, what does the context class do?
a) Implements the algorithm
b) Stores the strategy and delegates work to it
c) Instantiates concrete strategies
d) Observes changes in strategies
Which of the following is an advantage of the Strategy Pattern?
a) Reduces the number of classes in a system
b) Makes it easy to add new strategies without changing the context
c) Simplifies the implementation of algorithms
d) Decreases the number of objects in the system
What must be true about the different strategies in the Strategy Pattern?
a) They must share the same interface
b) They must be independent of each other
c) They must be singleton instances
d) They must be derived from the same class
In which of the following scenarios would the Strategy Pattern be appropriate?
a) When a class has multiple algorithms for performing an operation
b) When one object needs to change the state of another object
c) When you need to create multiple instances of a class
d) When object construction logic needs to be customized
Which of the following describes a key feature of the Strategy Pattern?
a) It allows dynamic switching between algorithms
b) It forces all algorithms to be fixed at compile-time
c) It uses global variables to store algorithms
d) It relies on inheritance for implementation
Section 5: Dependency Injection as a Design Pattern (6 Questions)
What is Dependency Injection in C#?
a) A method to instantiate dependencies at runtime
b) A way to pass dependencies from one class to another
c) A way to store dependencies globally
d) A way to simplify the creation of objects
Which of the following is a key benefit of using Dependency Injection?
a) Easier to test components in isolation
b) Higher memory usage
c) More complex to implement
d) Harder to maintain code
In Dependency Injection, how are dependencies typically provided to a class?
a) Through its constructor
b) Through static methods
c) Through public fields
d) Through method calls
Which of the following is a common approach to Dependency Injection in C#?
a) Constructor Injection
b) Static Injection
c) Field Injection
d) Method Injection
What is a potential downside of using Dependency Injection?
a) It can lead to tight coupling between components
b) It can make the system harder to test
c) It requires a lot of manual coding
d) It increases the amount of boilerplate code
Which of the following frameworks is commonly used in C# for Dependency Injection?
a) Entity Framework
b) ASP.NET Core
c) Windows Forms
d) ADO.NET
Answer Key
Qno
Answer
1
b) To ensure a class has only one instance and provide a global point of access
2
b) Only one instance exists in the system
3
b) Using a private constructor
4
b) lock
5
a) A static field
6
b) Global state can lead to tight coupling
7
a) To create objects without specifying the exact class of object that will be created
8
b) Factory method
9
c) Interface or abstract class
10
b) Easy to modify object creation logic
11
a) Dependency Injection
12
d) A parameter or configuration
13
a) To update all dependent objects when a subject changes
14
b) To maintain a list of observers
15
b) By subscribing to the subject’s events
16
a) Overloading the observers with too many notifications
17
a) One-to-many
18
a) Delegates and events
19
a) To allow different algorithms to be selected at runtime
20
b) Stores the strategy and delegates work to it
21
b) Makes it easy to add new strategies without changing the context
22
a) They must share the same interface
23
a) When a class has multiple algorithms for performing an operation
24
a) It allows dynamic switching between algorithms
25
b) A way to pass dependencies from one class to another
26
a) Easier to test components in isolation
27
a) Through its constructor
28
a) Constructor Injection
29
a) It can lead to tight coupling between components