Bird
Raised Fist0
C Sharp (C#)programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is a delegate in C# used for?
easy
A. To define variables that cannot change
B. To hold references to methods with a specific signature
C. To store values like integers or strings
D. To create new classes dynamically

Solution

  1. Step 1: Understand delegate purpose

    A delegate is a type that holds references to methods with a matching signature.
  2. Step 2: Compare options to definition

    Only To hold references to methods with a specific signature correctly describes this purpose; others describe unrelated concepts.
  3. Final Answer:

    To hold references to methods with a specific signature -> Option B
  4. Quick Check:

    Delegate = method reference holder [OK]
Hint: Delegates point to methods, not data or classes [OK]
Common Mistakes:
  • Confusing delegates with variables
  • Thinking delegates create classes
  • Assuming delegates store simple values
2. Which of the following is the correct way to declare a delegate that takes an int and returns void?
easy
A. void delegate MyDelegate(int number);
B. delegate int MyDelegate(void number);
C. delegate MyDelegate void(int number);
D. delegate void MyDelegate(int number);

Solution

  1. Step 1: Recall delegate declaration syntax

    The correct syntax is: delegate returnType DelegateName(parameterList);
  2. Step 2: Match syntax to options

    delegate void MyDelegate(int number); matches this exactly: delegate void MyDelegate(int number); others have incorrect order or types.
  3. Final Answer:

    delegate void MyDelegate(int number); -> Option D
  4. Quick Check:

    delegate returnType Name(params) = delegate void MyDelegate(int number); [OK]
Hint: Remember: 'delegate' keyword + return type + name + parameters [OK]
Common Mistakes:
  • Placing 'void' before 'delegate'
  • Swapping delegate name and return type
  • Using 'void' as parameter type
3. What will be the output of the following code?
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));
}
}
medium
A. 0
B. 34
C. 7
D. Compile error

Solution

  1. Step 1: Understand delegate assignment

    The delegate 'Operation' matches the method 'Add' signature (two ints, returns int), so 'op' points to 'Add'.
  2. Step 2: Evaluate delegate invocation

    Calling 'op(3, 4)' calls 'Add(3, 4)', which returns 3 + 4 = 7.
  3. Final Answer:

    7 -> Option C
  4. Quick Check:

    Delegate calls Add(3,4) = 7 [OK]
Hint: Delegate calls assigned method with given arguments [OK]
Common Mistakes:
  • Thinking delegate concatenates numbers
  • Expecting string output
  • Assuming compile error due to delegate
4. Identify the error in the following delegate instantiation:
delegate void Printer(string message);
class Program {
static void PrintMessage() { Console.WriteLine("Hello"); }
static void Main() {
Printer p = PrintMessage;
p("Hi");
}
}
medium
A. PrintMessage method signature does not match delegate
B. Delegate cannot be assigned to a method
C. Missing parentheses when assigning delegate
D. Delegate must return a value

Solution

  1. Step 1: Compare delegate and method signatures

    The delegate 'Printer' expects a method with a string parameter and void return type.
  2. Step 2: Check method signature

    'PrintMessage' has no parameters, so it does not match the delegate signature.
  3. Final Answer:

    PrintMessage method signature does not match delegate -> Option A
  4. Quick Check:

    Delegate and method signatures must match [OK]
Hint: Delegate and method signatures must match exactly [OK]
Common Mistakes:
  • Ignoring parameter mismatch
  • Thinking delegate needs parentheses on assignment
  • Assuming delegate must return a value
5. Given the delegate declaration:
delegate bool Filter(int value);

Which of the following correctly instantiates the delegate to a method that returns true if the number is even?
hard
A. Filter f = x => x % 2 == 0;
B. Filter f = (int x) => { return x * 2; };
C. Filter f = delegate(int x) { Console.WriteLine(x); };
D. Filter f = x => x + 2 == 0;

Solution

  1. Step 1: Understand delegate signature

    'Filter' expects a method taking an int and returning a bool.
  2. Step 2: Check each option's return type and logic

    Filter f = x => x % 2 == 0; returns true if x is even (x % 2 == 0), matching bool return.
    Filter f = (int x) => { return x * 2; }; returns int, not bool.
    Filter f = delegate(int x) { Console.WriteLine(x); }; returns void (no return), invalid.
    Filter f = x => x + 2 == 0; returns bool but checks x + 2 == 0, which is not the even check.
  3. Final Answer:

    Filter f = x => x % 2 == 0; -> Option A
  4. Quick Check:

    Delegate needs bool return matching even check [OK]
Hint: Match delegate return type and logic exactly [OK]
Common Mistakes:
  • Returning wrong type (int instead of bool)
  • Missing return statement in anonymous method
  • Incorrect condition for even check
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard