0
0
C Sharp (C#)programming~20 mins

Delegate declaration and instantiation in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Delegate Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of delegate invocation
What is the output of this C# code when the delegate is invoked?
C Sharp (C#)
delegate int Operation(int x, int y);

class Program {
    static int Add(int a, int b) => a + b;
    static void Main() {
        Operation op = Add;
        Console.WriteLine(op(3, 4));
    }
}
A7
B34
C0
DCompilation error
Attempts:
2 left
💡 Hint
Remember that the delegate points to the Add method which sums two integers.
Predict Output
intermediate
2:00remaining
Delegate with anonymous method output
What will be printed when this C# program runs?
C Sharp (C#)
delegate string Formatter(string s);

class Program {
    static void Main() {
        Formatter f = delegate(string input) { return input.ToUpper(); };
        Console.WriteLine(f("hello"));
    }
}
AHELLO
Bhello
CHello
DCompilation error
Attempts:
2 left
💡 Hint
The anonymous method converts the string to uppercase.
🔧 Debug
advanced
3:00remaining
Identify the error in delegate instantiation
Which option correctly fixes the error in this delegate instantiation code?
C Sharp (C#)
delegate void Printer(string message);

class Program {
    static void PrintMessage() {
        Console.WriteLine("Hello World");
    }
    static void Main() {
        Printer p = PrintMessage;
        p("Test");
    }
}
AChange delegate return type to string: delegate string Printer(string message);
BRemove the parameter from delegate Printer: delegate void Printer();
CCall p() without arguments: p();
DChange PrintMessage to accept a string parameter: static void PrintMessage(string msg)
Attempts:
2 left
💡 Hint
The delegate expects a method with one string parameter, but PrintMessage has none.
Predict Output
advanced
2:00remaining
Multicast delegate invocation output
What is the output of this C# program?
C Sharp (C#)
delegate void Notify();

class Program {
    static void Alert1() => Console.WriteLine("Alert 1");
    static void Alert2() => Console.WriteLine("Alert 2");
    static void Main() {
        Notify n = Alert1;
        n += Alert2;
        n();
    }
}
AAlert 2\nAlert 1
BAlert 1\nAlert 2
CAlert 1
DCompilation error
Attempts:
2 left
💡 Hint
Multicast delegates call all methods in the order they were added.
🧠 Conceptual
expert
3:00remaining
Delegate instantiation with lambda expressions
Which option correctly declares and instantiates a delegate that takes two integers and returns their product using a lambda expression?
AFunc multiply = (int x, int y) => x * y;
Bdelegate int multiply(int x, int y) => x * y;
CFunc<int, int, int> multiply = (x, y) => x * y;
Dvar multiply = delegate(int x, int y) { return x + y; };
Attempts:
2 left
💡 Hint
Use Func<> for delegates with return values and lambda syntax for instantiation.