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

Named arguments in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Named Arguments Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of method call with named arguments
What is the output of this C# code using named arguments?
C Sharp (C#)
using System;
class Program {
    static void PrintInfo(string name, int age, string city) {
        Console.WriteLine($"Name: {name}, Age: {age}, City: {city}");
    }
    static void Main() {
        PrintInfo(age: 30, city: "Paris", name: "Alice");
    }
}
AName: age, Age: 30, City: Paris
BName: Alice, Age: city, City: 30
CName: , Age: 0, City:
DName: Alice, Age: 30, City: Paris
Attempts:
2 left
💡 Hint
Named arguments allow you to specify parameters in any order by naming them explicitly.
❓ Predict Output
intermediate
2:00remaining
Output with mixed positional and named arguments
What will this C# program print?
C Sharp (C#)
using System;
class Program {
    static void Display(string first, string second, string third) {
        Console.WriteLine($"{first} - {second} - {third}");
    }
    static void Main() {
        Display("One", third: "Three", second: "Two");
    }
}
AOne - Two - Three
BOne - Three - Two
CThree - Two - One
DTwo - One - Three
Attempts:
2 left
💡 Hint
Positional arguments must come before named arguments.
🔧 Debug
advanced
2:00remaining
Identify the error with named arguments
What error does this C# code produce?
C Sharp (C#)
class Program {
    static void Foo(int x, int y) {}
    static void Main() {
        Foo(y: 5, 10);
    }
}
ARuntime error: ArgumentException
BCompile-time error: positional argument follows named argument
CCompile-time error: missing argument for parameter 'x'
DNo error, runs successfully
Attempts:
2 left
💡 Hint
In C#, positional arguments must come before named arguments.
🧠 Conceptual
advanced
2:00remaining
Effect of named arguments on method overload resolution
Given these method overloads, which call will compile without error?
C Sharp (C#)
class Program {
    static void Bar(int x, int y) {}
    static void Bar(int x, string y) {}
    static void Main() {
        // Which call is valid?
    }
}
ABar(x: 5, y: 10);
BBar(y: 10, x: 5);
CBar(x: 5, y: "hello");
DBar(y: "hello", x: 5);
Attempts:
2 left
💡 Hint
Named arguments do not change the types expected by the overloads.
❓ Predict Output
expert
2:00remaining
Output of method with optional and named arguments
What is the output of this C# program?
C Sharp (C#)
using System;
class Program {
    static void PrintDetails(string name, int age = 20, string city = "Unknown") {
        Console.WriteLine($"Name: {name}, Age: {age}, City: {city}");
    }
    static void Main() {
        PrintDetails(city: "Berlin", name: "Bob");
    }
}
AName: Bob, Age: 20, City: Berlin
BName: Bob, Age: 0, City: Berlin
CName: Bob, Age: , City: Berlin
DName: , Age: 20, City: Berlin
Attempts:
2 left
💡 Hint
Optional parameters use default values if not provided.

Practice

(1/5)
1. What is the main benefit of using named arguments in C#?
easy
A. It makes the program run faster.
B. You can specify which parameter gets which value by name, improving readability.
C. It allows you to skip parameters without default values.
D. It automatically converts data types.

Solution

  1. Step 1: Understand named arguments purpose

    Named arguments let you assign values to parameters by their names instead of relying on order.
  2. Step 2: Identify the benefit

    This makes code easier to read and understand because you see which value goes to which parameter.
  3. Final Answer:

    You can specify which parameter gets which value by name, improving readability. -> Option B
  4. Quick Check:

    Named arguments improve readability = A [OK]
Hint: Named arguments improve code clarity by naming parameters [OK]
Common Mistakes:
  • Thinking named arguments speed up execution
  • Assuming named arguments skip required parameters
  • Believing named arguments convert types automatically
2. Which of the following is the correct syntax to call a method PrintInfo with named arguments name and age?
easy
A. PrintInfo(name => "Alice", age => 30);
B. PrintInfo("Alice": name, 30: age);
C. PrintInfo(name = "Alice", age = 30);
D. PrintInfo(name: "Alice", age: 30);

Solution

  1. Step 1: Recall named argument syntax

    In C#, named arguments use the format parameterName: value.
  2. Step 2: Check each option

    PrintInfo(name: "Alice", age: 30); uses correct syntax. Options A, B, and C use invalid syntax for named arguments.
  3. Final Answer:

    PrintInfo(name: "Alice", age: 30); -> Option D
  4. Quick Check:

    Named argument syntax = parameterName: value [OK]
Hint: Use colon ':' between parameter and value for named arguments [OK]
Common Mistakes:
  • Using '=' instead of ':' for named arguments
  • Placing value before parameter name
  • Using '=>' instead of ':'
3. What will be the output of the following code?
void ShowDetails(string name, int age) {
    Console.WriteLine($"Name: {name}, Age: {age}");
}

ShowDetails(age: 25, name: "Bob");
medium
A. Compilation error due to argument order
B. Name: 25, Age: Bob
C. Name: Bob, Age: 25
D. Runtime error

Solution

  1. Step 1: Understand named argument order flexibility

    Named arguments allow passing parameters in any order by specifying names explicitly.
  2. Step 2: Analyze the call

    The call uses age: 25 first and name: "Bob" second, which is valid and assigns correctly.
  3. Final Answer:

    Name: Bob, Age: 25 -> Option C
  4. Quick Check:

    Named arguments allow any order = C [OK]
Hint: Named arguments can be in any order if names are used [OK]
Common Mistakes:
  • Assuming positional order is required with named arguments
  • Confusing parameter names and values
  • Expecting errors from reversed argument order
4. Identify the error in this method call:
void Display(string title, int count) { }

Display(count: 5, "Hello");
medium
A. Positional argument cannot follow named argument.
B. Missing semicolon after method call.
C. Parameter names are incorrect.
D. No error, code is correct.

Solution

  1. Step 1: Recall argument order rules

    In C#, positional arguments must come before any named arguments.
  2. Step 2: Analyze the call

    The call uses a named argument count: 5 first, then a positional argument "Hello", which is invalid.
  3. Final Answer:

    Positional argument cannot follow named argument. -> Option A
  4. Quick Check:

    Positional before named = B [OK]
Hint: Always put positional arguments before named ones [OK]
Common Mistakes:
  • Placing positional arguments after named arguments
  • Ignoring argument order rules
  • Assuming named arguments can be anywhere
5. Given the method:
void Configure(string mode, int timeout = 30, bool verbose = false) { }

Which call correctly uses named arguments to set verbose to true and keep default timeout?
hard
A. Configure("fast", verbose: true);
B. Configure(verbose: true, "fast");
C. Configure(timeout: 30, "fast", verbose: true);
D. Configure("fast", true);

Solution

  1. Step 1: Understand default parameters and named arguments

    Default parameters can be skipped if not specified. Named arguments let you set specific parameters by name.
  2. Step 2: Analyze each call

    Configure("fast", verbose: true); sets mode positionally and verbose by name, skipping timeout to use default. Configure(verbose: true, "fast"); has positional after named (invalid). Configure(timeout: 30, "fast", verbose: true); mixes positional and named incorrectly. Configure("fast", true); sets verbose positionally, which is wrong type.
  3. Final Answer:

    Configure("fast", verbose: true); -> Option A
  4. Quick Check:

    Named arguments skip defaults correctly = A [OK]
Hint: Use named arguments to skip default parameters safely [OK]
Common Mistakes:
  • Placing positional arguments after named arguments
  • Passing wrong types positionally
  • Trying to set named arguments before positional ones
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