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

Action delegate type in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Action Delegate Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Action delegate with multiple methods
What is the output of this C# code using an Action delegate with multiple methods?
C Sharp (C#)
using System;

class Program {
    static void Main() {
        Action action = Method1;
        action += Method2;
        action();
    }

    static void Method1() {
        Console.WriteLine("Hello");
    }

    static void Method2() {
        Console.WriteLine("World");
    }
}
AWorld\nHello
BHello\nWorld
CHello
DCompilation error
Attempts:
2 left
💡 Hint
Think about how Action delegates combine multiple methods and invoke them in order.
Predict Output
intermediate
1:30remaining
Action delegate with parameters output
What will be printed when this C# program runs?
C Sharp (C#)
using System;

class Program {
    static void Main() {
        Action<string> greet = name => Console.WriteLine($"Hi, {name}!");
        greet("Alice");
    }
}
AHi, Alice!
BCompilation error
CHi, !
DHi, {name}!
Attempts:
2 left
💡 Hint
Look at how the lambda uses the parameter to print the greeting.
🔧 Debug
advanced
2:00remaining
Why does this Action delegate cause a runtime error?
Consider this code snippet. Why does it throw a NullReferenceException at runtime?
C Sharp (C#)
using System;

class Program {
    static Action action;

    static void Main() {
        action();
    }
}
AThe Action delegate 'action' is null because it was never assigned a method.
BThe Action delegate cannot be static.
CThe Main method must return void, not int.
DThe Action delegate requires parameters to be invoked.
Attempts:
2 left
💡 Hint
Check if the delegate variable was assigned before calling it.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in Action delegate declaration
Which option contains a syntax error when declaring an Action delegate?
AAction<int> printNumber = n => Console.WriteLine(n);
BAction printHello = () => Console.WriteLine("Hello");
CAction<int> action = (int n) => { Console.WriteLine(n); }
DAction<string> greet = (string name) => Console.WriteLine(name);
Attempts:
2 left
💡 Hint
Check the syntax inside the lambda body for missing punctuation.
🚀 Application
expert
2:30remaining
Using Action delegate to modify a list
Given this code, what is the final content of the list 'numbers' after invoking the Action delegate?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        List<int> numbers = new() {1, 2, 3};
        Action<List<int>> addNumbers = list => {
            list.Add(4);
            list.Add(5);
        };
        addNumbers(numbers);
        numbers.Remove(2);
        addNumbers(numbers);
        Console.WriteLine(string.Join(",", numbers));
    }
}
A1,3,4,5,2
B1,2,3,4,5,4,5
C1,3,4,5
D1,3,4,5,4,5
Attempts:
2 left
💡 Hint
Remember the list is modified twice by the Action delegate and once by Remove.