C# Delegate & Event MCQs

Question 1: Which of the following statements are TRUE?

Question 2: How to define a delegate which points to the following methods?

public static string ToNumberString(int num)
{
    return String.Format("{0,-12:N0}", num);
}

public static string ToMoneyString(int money)
{
    return String.Format("$ {0:C}", money);
}

Question 3: How to pass a function as a parameter in C#?

Question 4: What will be the output of the following program?

public delegate void Display(int value);

static void Main(string[] args)
{
    Display displayDel = DisplayNum;
    displayDel += DisplayMoney;

    displayDel.Invoke(100000);
}
public static void DisplayNum(int num)
{
    Console.WriteLine("Number: {0,-12:N0}", num);
}

public static void DisplayMoney(int money)
{
    Console.WriteLine("Money: {0:C}", money);
}

Question 5: Which of the following statement is TRUE?

Question 6: How would you declare an event for the following delegate?

	public delegate void ClickHandler();

Question 7: Which of the following is the built-in delegate function for handling events in .NET?

Question 8: What will be the output of the following program?

class Program
{
    static void Main(string[] args)
    {
        MyClass mc = new MyClass();

        mc.MyEvent += new EventHandler(mc.MyClassEventHandler);
        mc.MyEvent -= null;
    }
}

class MyClass
{
    public event EventHandler MyEvent
    {
        add
        {
            Console.Write("Add events here");
        }

        remove
        {
            Console.Write("Remove events here");
        }
    }

    public void MyClassEventHandler(object sender, EventArgs e)
    {
    }
}

Question 9: Events can also be declared static, virtual, sealed, and abstract.

Question 10: Which of the following can contain event declarations?