MCQs on C# Delegates and Events | C#

Enhance your C# skills with 30 multiple-choice questions on Delegates and Events. Topics include delegate types (Action, Func, Predicate), event handling, multicast delegates, and lambda expressions.


Chapter 9: C# Delegates and Events

Understanding Delegates

  1. What is a delegate in C#?
    • a) A pointer to a method
    • b) A reference to an object
    • c) A class that performs calculations
    • d) A collection of methods
  2. Which keyword is used to declare a delegate in C#?
    • a) delegate
    • b) event
    • c) void
    • d) function
  3. How do you instantiate a delegate in C#?
    • a) Using the new keyword with a method reference
    • b) Using the delegate keyword only
    • c) By assigning a method to a variable
    • d) Using a constructor
  4. What does a delegate allow in C#?
    • a) It allows calling a method indirectly
    • b) It binds multiple methods together
    • c) It allows function overloading
    • d) It performs arithmetic operations
  5. Which of the following is true about delegates in C#?
    • a) Delegates are type-safe
    • b) Delegates can only reference methods with void return type
    • c) Delegates are not flexible
    • d) Delegates cannot be used in events
  6. Which method can be called using a delegate?
    • a) Any method matching the delegate signature
    • b) Only methods with no parameters
    • c) Only methods that return a value
    • d) Only static methods

Delegate Types (Action, Func, Predicate)

  1. What is the purpose of the Action delegate in C#?
    • a) It represents methods that return void and take parameters
    • b) It represents methods that return a value and take parameters
    • c) It represents methods with no parameters
    • d) It represents methods that take no parameters and return a value
  2. What is the main difference between Func and Action delegates in C#?
    • a) Func returns a value, while Action does not
    • b) Func takes no parameters, while Action takes parameters
    • c) Func is used for event handling, while Action is not
    • d) Func is used for asynchronous methods, while Action is not
  3. What is the Predicate delegate used for in C#?
    • a) It represents methods that take a parameter and return a boolean
    • b) It represents methods that take no parameters and return a boolean
    • c) It represents methods that return an integer
    • d) It represents methods that perform mathematical operations
  4. Which delegate type would be used for a method that takes an integer parameter and returns a string in C#?
  • a) Func<int, string>
  • b) Action<int>
  • c) Predicate<int>
  • d) Action<string>
  1. How many parameters can a Func delegate take in C#?
  • a) Up to 16 parameters
  • b) Only one parameter
  • c) Unlimited parameters
  • d) None, it only returns a value
  1. Which delegate type would be appropriate to use for a method that takes no parameters and returns nothing in C#?
  • a) Action
  • b) Func
  • c) Predicate
  • d) EventHandler
  1. What is the maximum number of parameters a Predicate delegate can take in C#?
  • a) One
  • b) Two
  • c) Three
  • d) Unlimited
  1. Can a Func delegate be used to represent a method that does not return a value?
  • a) No, it must always return a value
  • b) Yes, but the return type must be void
  • c) Yes, the return type can be anything
  • d) No, it requires a return value

Event Handling

  1. What is the main purpose of events in C#?
  • a) To allow methods to be triggered by other objects
  • b) To store data
  • c) To handle exceptions
  • d) To store user inputs
  1. How are events declared in C#?
  • a) Using the event keyword
  • b) Using the delegate keyword
  • c) Using the public keyword
  • d) Using the handler keyword
  1. What is the relationship between events and delegates in C#?
  • a) Events use delegates to handle method invocations
  • b) Events replace delegates
  • c) Events are declared in a method
  • d) Delegates cannot be used in events
  1. Which of the following is true about event handlers in C#?
  • a) Event handlers are methods that are called when an event is triggered
  • b) Event handlers can only be invoked by delegates
  • c) Event handlers cannot be invoked directly
  • d) Event handlers cannot return a value
  1. How can a method subscribe to an event in C#?
  • a) By using the += operator
  • b) By using the = operator
  • c) By using the add keyword
  • d) By using the subscribe method
  1. What happens if an event has no subscribers in C#?
  • a) The event will never be triggered
  • b) The event will throw an exception
  • c) The event will execute an empty method
  • d) The event will trigger an error
  1. Can an event be triggered by multiple subscribers in C#?
  • a) Yes, an event can have multiple subscribers
  • b) No, only one subscriber can be attached to an event
  • c) Yes, but only with multicast delegates
  • d) No, events cannot have multiple subscribers
  1. What is the purpose of the EventHandler delegate in C#?
  • a) To represent methods that handle events
  • b) To trigger events
  • c) To subscribe to events
  • d) To call methods asynchronously

Multicast Delegates

  1. What is a multicast delegate in C#?
  • a) A delegate that can hold references to multiple methods
  • b) A delegate that only holds one method
  • c) A delegate used for async operations
  • d) A delegate that triggers events
  1. How do you remove a method from a multicast delegate in C#?
  • a) Using the -= operator
  • b) Using the remove keyword
  • c) Using the unsubscribe method
  • d) By calling the Clear() method
  1. What happens when a multicast delegate is invoked in C#?
  • a) All methods in the delegate invocation list are called in order
  • b) Only the first method in the list is called
  • c) Only the last method in the list is called
  • d) No methods are called
  1. Can a delegate have both Action and Func methods in its invocation list in C#?
  • a) Yes, as long as the return types match
  • b) No, delegates cannot have mixed return types
  • c) Yes, but only if the parameters are the same
  • d) No, it is not allowed

Lambda Expressions

  1. What is the syntax for a lambda expression in C#?
  • a) (parameter) => expression
  • b) parameter => expression
  • c) expression => parameter
  • d) parameter -> expression
  1. How do lambda expressions improve code readability in C#?
  • a) They allow writing inline methods for simple operations
  • b) They require less memory than regular methods
  • c) They allow using multiple delegates in one statement
  • d) They automatically handle event subscriptions
  1. What is the return type of a lambda expression in C#?
  • a) It can be inferred based on the expression
  • b) It must always be void
  • c) It must be explicitly specified
  • d) It must always be a boolean
  1. What does the => symbol in a lambda expression represent in C#?
  • a) It separates the parameters from the expression
  • b) It assigns the result of an expression to a variable
  • c) It indicates the type of the expression
  • d) It separates multiple expressions in a delegate

Answer Key

QnoAnswer
1a) A pointer to a method
2a) delegate
3a) Using the new keyword with a method reference
4a) It allows calling a method indirectly
5a) Delegates are type-safe
6a) Any method matching the delegate signature
7a) It represents methods that return void and take parameters
8a) Func returns a value, while Action does not
9a) It represents methods that take a parameter and return a boolean
10a) Func<int, string>
11a) Up to 16 parameters
12a) Action
13a) One
14a) No, it must always return a value
15a) To allow methods to be triggered by other objects
16a) Using the event keyword
17a) Events use delegates to handle method invocations
18a) Event handlers are methods that are called when an event is triggered
19a) By using the += operator
20a) The event will never be triggered
21a) Yes, an event can have multiple subscribers
22a) To represent methods that handle events
23a) A delegate that can hold references to multiple methods
24a) Using the -= operator
25a) All methods in the delegate invocation list are called in order
26a) Yes, as long as the return types match
27a) (parameter) => expression
28a) They allow writing inline methods for simple operations
29a) It can be inferred based on the expression
30a) It separates the parameters from the expression

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top