Challenge - 5 Problems
Delegate Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)); } }
Attempts:
2 left
💡 Hint
Remember that the delegate points to the Add method which sums two integers.
✗ Incorrect
The delegate 'Operation' is assigned the method 'Add'. Calling op(3,4) invokes Add(3,4), which returns 7.
❓ Predict Output
intermediate2: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")); } }
Attempts:
2 left
💡 Hint
The anonymous method converts the string to uppercase.
✗ Incorrect
The delegate 'Formatter' is assigned an anonymous method that returns the uppercase version of the input string. So 'hello' becomes 'HELLO'.
🔧 Debug
advanced3: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"); } }
Attempts:
2 left
💡 Hint
The delegate expects a method with one string parameter, but PrintMessage has none.
✗ Incorrect
The delegate 'Printer' requires a method that takes a string parameter. The method 'PrintMessage' has no parameters, so it must be changed to accept a string.
❓ Predict Output
advanced2: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(); } }
Attempts:
2 left
💡 Hint
Multicast delegates call all methods in the order they were added.
✗ Incorrect
The delegate 'n' calls Alert1 first, then Alert2, so both messages print in that order.
🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
Use Func<> for delegates with return values and lambda syntax for instantiation.
✗ Incorrect
Option C correctly uses Func delegate type and a lambda expression returning the product. Option C is invalid syntax. Option C misses generic parameters. Option C returns sum, not product.