Challenge - 5 Problems
Multicast Delegate Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Multicast Delegate Invocation
What is the output of the following C# code using multicast delegates?
C Sharp (C#)
using System; public delegate void Notify(); class Program { static void Main() { Notify notify = MethodA; notify += MethodB; notify(); } static void MethodA() => Console.WriteLine("MethodA called"); static void MethodB() => Console.WriteLine("MethodB called"); }
Attempts:
2 left
💡 Hint
Multicast delegates invoke methods in the order they were added.
✗ Incorrect
When invoking a multicast delegate, all methods are called in the order they were added. Here, MethodA is added first, then MethodB, so MethodA runs before MethodB.
❓ Predict Output
intermediate2:00remaining
Return value of multicast delegate with int return type
What is the output of this C# code that uses a multicast delegate with an int return type?
C Sharp (C#)
using System; public delegate int Compute(int x); class Program { static void Main() { Compute compute = Square; compute += Double; int result = compute(3); Console.WriteLine(result); } static int Square(int x) => x * x; static int Double(int x) => x * 2; }
Attempts:
2 left
💡 Hint
Multicast delegates with return values return the last method's result.
✗ Incorrect
When a multicast delegate has a return type, only the return value of the last invoked method is returned. Here, Square(3) returns 9 (discarded), Double(3) returns 6, so the delegate returns 6, which is printed.
❓ Predict Output
advanced2:00remaining
Effect of removing a method from a multicast delegate
What is the output of this C# program after removing a method from a multicast delegate?
C Sharp (C#)
using System; public delegate void Notify(); class Program { static void Main() { Notify notify = MethodA; notify += MethodB; notify -= MethodA; notify(); } static void MethodA() => Console.WriteLine("MethodA called"); static void MethodB() => Console.WriteLine("MethodB called"); }
Attempts:
2 left
💡 Hint
Removing a method unsubscribes it from the delegate invocation list.
✗ Incorrect
After removing MethodA, only MethodB remains in the delegate. So only MethodB is called, producing "MethodB called".
❓ Predict Output
advanced2:00remaining
Exception behavior in multicast delegates
What happens when a multicast delegate invokes multiple methods and one throws an exception?
C Sharp (C#)
using System; public delegate void Notify(); class Program { static void Main() { Notify notify = MethodA; notify += MethodB; try { notify(); } catch (Exception ex) { Console.WriteLine("Exception caught: " + ex.Message); } } static void MethodA() => Console.WriteLine("MethodA called"); static void MethodB() => throw new InvalidOperationException("Error in MethodB"); }
Attempts:
2 left
💡 Hint
Multicast delegates invoke methods in order until an exception occurs.
✗ Incorrect
MethodA runs and prints its message. Then MethodB throws an exception, which stops further invocation. The exception is caught and its message printed.
🧠 Conceptual
expert2:00remaining
Number of methods in a multicast delegate
Given the following code, how many methods are in the multicast delegate 'notify' before invocation?
C Sharp (C#)
using System; public delegate void Notify(); class Program { static void Main() { Notify notify = MethodA; notify += MethodB; notify += MethodC; notify -= MethodB; notify += MethodD; } static void MethodA() { } static void MethodB() { } static void MethodC() { } static void MethodD() { } }
Attempts:
2 left
💡 Hint
Count additions and removals carefully.
✗ Incorrect
Initially, notify has MethodA (1). Then MethodB added (2), MethodC added (3). MethodB removed (back to 2). MethodD added (3). So total 3 methods before invocation.