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

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

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

Practice

(1/5)
1. What is the primary purpose of the Action delegate type in C#?
easy
A. To represent methods that return void and can take parameters
B. To represent methods that return a value
C. To represent asynchronous methods only
D. To represent methods that take no parameters and return a value

Solution

  1. Step 1: Understand what Action represents

    Action is a delegate type used to represent methods that do not return a value (void) but can take parameters.
  2. Step 2: Compare options with the definition

    To represent methods that return void and can take parameters correctly states that Action represents methods returning void and can take parameters. Other options mention return values or asynchronous behavior, which are incorrect.
  3. Final Answer:

    To represent methods that return void and can take parameters -> Option A
  4. Quick Check:

    Action = void methods with parameters [OK]
Hint: Action always means void return with optional parameters [OK]
Common Mistakes:
  • Confusing Action with Func which returns a value
  • Thinking Action is only for methods without parameters
  • Assuming Action is for async methods only
2. Which of the following is the correct syntax to declare an Action delegate that takes two integer parameters?
easy
A. Action myAction(int, int);
B. Action<int> myAction;
C. Action myAction;
D. Action myAction() => void;

Solution

  1. Step 1: Recall Action delegate syntax

    The Action delegate syntax for parameters is Action<T1, T2> for two parameters.
  2. Step 2: Analyze each option

    Action myAction; correctly declares an Action with two int parameters. Action<int> myAction; has only one parameter. Action myAction(int, int); is invalid syntax. Action myAction() => void; is incorrect syntax and return type usage.
  3. Final Answer:

    Action myAction; -> Option C
  4. Quick Check:

    Two parameters use Action<int, int> [OK]
Hint: Use Action for two parameters [OK]
Common Mistakes:
  • Using parentheses like a method instead of generic syntax
  • Declaring return type with Action (it always returns void)
  • Confusing Action with Func syntax
3. What will be the output of the following C# code?
Action<string> greet = name => Console.WriteLine($"Hello, {name}!");
greet("Alice");
medium
A. Hello, name!
B. Hello, Alice!
C. greet
D. No output

Solution

  1. Step 1: Understand the Action delegate usage

    The greet variable is an Action<string> delegate that takes a string and prints a greeting with that string.
  2. Step 2: Trace the method call

    Calling greet("Alice") runs the lambda, printing "Hello, Alice!" to the console.
  3. Final Answer:

    Hello, Alice! -> Option B
  4. Quick Check:

    Action<string> prints Hello, Alice! [OK]
Hint: Action runs the method body with given parameters [OK]
Common Mistakes:
  • Expecting variable name printed instead of parameter value
  • Thinking Action returns a value to print
  • Missing that Console.WriteLine outputs text
4. Identify the error in the following code snippet:
Action<int> printNumber = (int num) => return num.ToString();
medium
A. Lambda expression cannot use return with Action delegate
B. Action delegate cannot have parameters
C. Missing semicolon after lambda
D. Action delegate must return a string

Solution

  1. Step 1: Understand Action delegate return type

    Action delegates always return void, so their lambda expressions cannot return a value.
  2. Step 2: Analyze the lambda expression

    The lambda tries to return num.ToString(), which is a string, causing a mismatch with void return type.
  3. Final Answer:

    Lambda expression cannot use return with Action delegate -> Option A
  4. Quick Check:

    Action = void, no return allowed [OK]
Hint: Action lambdas must not return values [OK]
Common Mistakes:
  • Trying to return a value from an Action delegate
  • Confusing Action with Func delegate
  • Ignoring lambda syntax rules
5. You want to create an Action delegate that takes three parameters: an int, a string, and a bool. It should print a message only if the bool is true. Which of the following code snippets correctly implements this?
hard
A. Action printIfTrue = (num, text) => { if(flag) Console.WriteLine($"{num}: {text}"); };
B. Action printIfTrue = (num, text, flag) => { if(flag) return Console.WriteLine($"{num}: {text}"); };
C. Action printIfTrue = (num, text, flag) => Console.WriteLine($"{num}: {text}");
D. Action printIfTrue = (num, text, flag) => { if(flag) Console.WriteLine($"{num}: {text}"); };

Solution

  1. Step 1: Check parameter count and types

    Action printIfTrue = (num, text, flag) => { if(flag) Console.WriteLine($"{num}: {text}"); }; correctly declares an Action with three parameters: int, string, and bool. Action printIfTrue = (num, text) => { if(flag) Console.WriteLine($"{num}: {text}"); }; has only two parameters, so it's invalid.
  2. Step 2: Verify the lambda body logic

    Action printIfTrue = (num, text, flag) => { if(flag) Console.WriteLine($"{num}: {text}"); }; uses an if statement to print only if the bool is true, which matches the requirement. Action printIfTrue = (num, text, flag) => { if(flag) return Console.WriteLine($"{num}: {text}"); }; incorrectly uses return with Console.WriteLine, which is invalid for Action. Action printIfTrue = (num, text, flag) => Console.WriteLine($"{num}: {text}"); always prints regardless of the bool.
  3. Final Answer:

    Action printIfTrue = (num, text, flag) => { if(flag) Console.WriteLine($"{num}: {text}"); }; -> Option D
  4. Quick Check:

    Correct params and conditional print = Action printIfTrue = (num, text, flag) => { if(flag) Console.WriteLine($"{num}: {text}"); }; [OK]
Hint: Match parameters and avoid return in Action lambdas [OK]
Common Mistakes:
  • Using return inside Action lambda
  • Mismatching parameter count
  • Ignoring the bool condition in printing
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