Sample questions
Question 1Medium
Which of the following are the correct ways to declare a delegate for calling the function func() defined in the sample class given below?
class Sample
{
public int func(int i, Single j)
{
}
}
- delegate d(int i, Single j);
- delegate void d(int, Single);
- delegate int d(int i, Single j);
- delegate void (int i, Single j);
Answer: C. delegate int d(int i, Single j);
Explanation
The Purpose of delegate object can be used in to code which can call the referenced method, without having to know at compile time which method will be invoked. For that the signature of the delegate (including no. of parameters with data type to be matched)
Step-by-step solution
- Delegate must match return type and parameters of func().
- func() returns int and takes int i, Single j.
- So delegate should be 'delegate int d(int i, Single j);'.
- Option C matches this signature exactly.
- Final answer: delegate int d(int i, Single j);
Why the other options are wrong
- A. Delegate must specify return type of func().
- B. Delegate is void but func() returns int.
- D. Delegate syntax is incorrect; missing name.
Tip: Always check return type and parameter list when declaring delegates.
Question 2Medium
Is the following true about predicate? Represents the method that defines a setof criteria and determines whether the specified object meets those criteria
- Yes
- No
Answer: A. Yes
Explanation
Yes. A Predicate<T> is a delegate that takes one argument and returns a bool saying whether it meets the criteria. For Predicate<int> predicate = value => value == 5;, Invoke(4) returns false and Invoke(5) returns true, so the output is:
False
True
Step-by-step solution
- Predicate is a generic delegate in the System namespace.
- It takes one argument and returns a bool.
- It represents a method that sets a criterion and tells whether an object meets it.
- The statement is true, so the answer is Yes: option A.
Why the other options are wrong
- B. The description matches Predicate<T>, so 'No' is wrong.
Tip: A Predicate is a delegate with one input and a true or false result, like Func with a bool output.
Question 3Medium
Is the following true about Event in c#?
- Events allow you to specify that when external event occurs, such as a mouse click, a delegate method should always be executed. Events are most commonly used in user interfaces such as windows forms.
- You can define custom events in any program.
- An event in c# is a way for a class to provide notification to clients of that class when some interesting thing happens to an object.
- All of the above.
Answer: D. All of the above.
Explanation
Events are basically a user action like key press, clicks, mouse movements, etc., or some occurrence like system generatednotifications. Applications need to respond to events when they occur.For example,interrupts. Events are used for inter-process communication.
The events are declared and raised in a class and associated with the event handlers using delegates within the same class or some other class. The class containing the event is used to publish the event. To declare an event inside a class, first a delegate type for the event must be declared. For example,
public delegate void Boiler LogHandler(string status);
Step-by-step solution
- Option C is the basic idea: a class notifies its clients when something interesting happens to an object.
- Option B is true: a program can define its own events.
- Option A describes the common use: a handler runs when an action such as a mouse click or key press occurs.
- All three are true, so the answer is option D, All of the above.
Why the other options are wrong
- A. This describes event use in UI, which is true, but it covers only part of what the question asks.
- B. Defining custom events in a program is true too, but on its own it is not the complete answer.
- C. A class notifying clients is true, but it is one of three true statements, so D is the full answer.
Tip: An event is a notice from a class to its clients; a delegate is the type that defines the handler signature.
Question 4Medium
- reference type
- value type
- both
- none of above
Answer: A. reference type
Explanation
A delegate is a class, so it is a reference type, not a value type.
Step-by-step solution
- Delegate holds a reference to a method, not the method itself.
- Reference types store memory addresses, not actual data.
- Methods are stored in memory and referenced by delegates.
- So delegate is a reference type.
- Final answer: reference type.
Why the other options are wrong
- B. Value types store actual data, not references to methods.
- C. Delegate cannot be both; it's strictly a reference type.
Tip: Remember delegates are always reference types.
Question 5Medium
What is a purpose of a delegate?
- To spawn an additional thread to provide parallel processing
- To copy member methods and properties from an existing class
- To enable an assembly to respond to an event that occurs within a class
- To provide identical member methods and properties from multiple related classes.
Answer: C. To enable an assembly to respond to an event that occurs within a class
Explanation
A delegate does not 'do' anything. Creating a delegate does not change anything. A delegate is just a type of variable. Delegates can be used without events.
Step-by-step solution
- Delegates are used to reference methods.
- Events in C# use delegates to notify subscribers.
- Subscribers respond to events by implementing delegate methods.
- Delegate enables a class to call method on another object.
- Thus, delegate allows an assembly to respond to an event.
Why the other options are wrong
- A. Spawning threads is done with threads, not delegates.
- B. Copying methods and properties is done with inheritance or interfaces.
- D. Providing identical members is achieved through inheritance or interfaces.
Tip: Remember delegates are method references used with events.
Question 6Medium
- a means of passing arrays into methods
- a substitute for an inherited method
- a class that encapsulates methods
- None of above
Answer: C. a class that encapsulates methods
Explanation
A delegate is a type that encapsulates one or more methods. You can invoke all the methods the delegate is encapsulating with a single call, whenever you wish.
Step-by-step solution
- A delegate is a type that encapsulates one or more methods.
- It can call all of those methods with a single invocation.
- So the correct answer, a class that encapsulates methods, is correct.
Why the other options are wrong
- A. Delegates wrap methods; they are not a way of passing arrays.
- B. A delegate is not a substitute for an inherited method.
- D. Option C is correct, so None of above is wrong.
Tip: A delegate is a type-safe reference to one or more methods.
Question 7Medium
An event declaration defines a type that encapsulates a method with a particular set of arguments and return type.
- True
- False
Answer: B. False
Explanation
Events help to solve the delegate encapsulation problem. Events sit on top of delegates and provide encapsulation so that the destination source can only listen and not have full control of the delegate object. The first problem with the way we are raising the event above is that Main() is raising an event directly that is defined in another class (namely, My Delegates).
This is not good design. It is My Delegates event, so that should be the only class that can raise it directly.
Step-by-step solution
- An event declaration does not define a new type but uses an existing delegate type.
- Events are based on delegates and do not create a new method type.
- The event itself is a wrapper around the delegate, not a new type definition.
- Raising events should be done by the class that defines them, not external classes.
- Therefore, the statement is false as it misrepresents what an event declaration does.
Why the other options are wrong
- A. Events do not define new types but use existing delegate types.
Question 8Medium
A(an)...is the encapsulation of the idea that "something happened" to which the program must respond.
- Event
- Delegate
- event-delegate model
- None of above
Answer: A. Event
Explanation
An event is the idea that something has happened and the program must respond to it. Events sit on top of delegates: they give encapsulation, so the listener can only subscribe and listen and cannot take full control of the delegate. Multicast delegates are the basis of the broadcast model, and events wrap them.
Step-by-step solution
- The question describes an event: something happened that the program must respond to.
- Events sit on top of delegates and wrap them so listeners can subscribe without controlling the delegate.
- So the correct answer, Event, is correct.
Why the other options are wrong
- B. A delegate is the type that holds method references, not the notification idea itself.
- C. The event-delegate model is the pattern using both, not the single term the question asks for.
- D. Event is the correct term, so None of above is wrong.
Tip: Event means notification ('something happened'); a delegate is the type that holds methods.
Practise all 8 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →