Working with Delegates and Events

Working with Delegates and Events


Q.1 Choose the correct option according to the given statements.

Statement 1: A delegate is an object that can refer to a method.
Statement 2: A delegate in C# is similar to a function pointer in C/C++.
Statement 3: The method that will be invoked by a delegate is determined at compile time.

A) Only statement 1 is correct.
B) Only statement 2 is correct.
C) Statement 1 and 2 are correct.
D) Only statement 3 is correct.

View Answer / Hide Answer

ANSWER: C




Q.2 What is general form of a delegate declaration?

A) delegate return-type name_of_delegate(parameter-list);
B) name_of_delegate(parameter-list);
C) delegate name_of_delegate;
D) None of the above.

View Answer / Hide Answer

ANSWER: A

Explanation:

A delegate type is declared using the keyword delegate. The general form of a delegate declaration is shown here:

delegate return-type name_of_delegate(parameter-list);

Here, return-type is the type of value returned by the methods that the delegate will be calling.

The name of the delegate is specified by name_of_delegate. The parameters required by the methods are specified by the parameter-list.




Q.3 Choose the correct option according to the given statements.

Statement 1: delegate can be used to call only those methods that signature and return type are same as delegate.
Statement 2: A delegate cannot refer to a static method.
Statement 3: A delegate can refer to a static method.

A) Statement 1 and 2 are correct.
B) Only statement 2 is correct.
C) Statement 1 and 3 are correct.
D) Only statement 3 is correct.

View Answer / Hide Answer

ANSWER: C




Q.4 A method is written as follows

string DemoFunction(string s)
{
- - - - - - -
- - - - - - -
}

Choose the correct code for delegate declaration to call above function.

A) delegate string name_of_delegate (string str);
B) string name_of_delegate (string str);
C) delegate string name_of_delegate (string s, string str);
D) None of the above.

Ans. A

View Answer / Hide Answer

ANSWER: A

Explanation:

delegate can call only those methods whose signature and return type are same as delegate.




Q.5 Choose the correct option regarding the multicast delegate.

Statement 1: multicasting is the ability to create an invocation list, or chain, of methods that will be automatically called when a delegate is invoked.
Statement 2: + or += operator is used to add methods to the chain for multicasting.
Statement 3: - or - = operator is used to remove methods from the chain.

A) Only statement 1 and 2 are correct.
B) Only statement 2 is correct.
C) Only statement 1 and 3 are correct.
D) All statements are correct.

View Answer / Hide Answer

ANSWER: D

Explanation: The given below example shows how multicast delegate can be used.

using System;

// Declare a delegate type.

delegate void Test();

class DelegateTest
{
public void function1()
{
Console.WriteLine("Function 1 is called");
}
public void function2()
{
Console.WriteLine("Function 2 is called");
}
}

class Program
{
static void Main(string[] args)
{
DelegateTest obj = new DelegateTest();
Test delObj = new Test(obj.function1);
// Set up multicast.
delObj += obj.function2;

delObj();
Console.WriteLine("Function 2 removed from the list");
delObj -= obj.function2;

delObj();
}
}

Output:

Function 1 is called
Function 2 is called
Function 2 removed from the list
Function 1 is called




Q.6 Choose the correct option about the Anonymous method.

A) Anonymous method cannot use Ref and Out parameter from outside of anonymous method.
B) Anonymous methods allow us to define a code block where a delegate object is acceptable.
C) Anonymous method can use Ref and Out parameter from outside of anonymous method.
D) Option A and B are correct.

View Answer / Hide Answer

ANSWER: D




Q.7 What is general form of an event declaration?

A) event event-name;
B) event delegateName event-name;
C) event ret-type event-name(parameter-list);
D) None of the above

View Answer / Hide Answer

ANSWER: B




Q.8 Choose the correct option according to the given statements about events and delegates.

A) events can be multicast.
B) delegate can be multicast.
C) events cannot be multicast.
D) Option A and B are correct

View Answer / Hide Answer

ANSWER: D




Q.9 According to above given code, how will you call function1 by using delegate?

delegate void Test();
class DelegateTest
{
public void function1()
{
Console.WriteLine("Function 1 called");
}

}

A) DelegateTest obj = new DelegateTest();
Test delObj =obj.function1;
delObj();

B) DelegateTest obj = new DelegateTest();
Test delObj = new Test (obj.function1);
delObj();

C) Option A and B both are correct.
D) None of the above.

View Answer / Hide Answer

ANSWER: C




Q. 10 Choose the correct option according to the given statements about lambda expression.
A) If the lambda body consists of a single expression, then an expression lambda is being created.
B) If the lambda body consists of a block of statements enclosed by braces, then a statement lambda is being created.
C) If the lambda body consists of a single expression, then an statement lambda is being created.
D) Option A and B are correct.

View Answer / Hide Answer

ANSWER: D

C# supports two types of lambda expression. It is the lambda body that determines what type is being created.

Expression Lambdas

The expression on the right side of the => acts on the parameter specified by the left side.

The general form of an expression lambda that takes only one parameter will be as follows:

param => expr

Here is a simple expression lambda:

count => count + 5

Here count is the parameter that is acted on by the expression count + 5. So the value of count increased by five.

Statement Lambdas

A statement lambda allows the body of lambda to contain multiple statements. In statement lambda you can use loops, if statements, declare variables, and so on.

Example:

// Declare a delegate type.

delegate int FactDemo(int f);

class Program
{
static void Main(string[] args)
{

FactDemo factorial = n =>
{
int num = 1;
for (int i = 1; i <= n; i++)
{
num = num * i;
}
return num;

};

Console.WriteLine("Factorial is =" +factorial(6));
}
}




Post your comment