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

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

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