Challenge - 5 Problems
Named Arguments Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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"); } }
Attempts:
2 left
💡 Hint
Named arguments allow you to specify parameters in any order by naming them explicitly.
✗ Incorrect
The method PrintInfo is called with named arguments, so the order does not matter. The values are assigned correctly to name, age, and city.
❓ Predict Output
intermediate2: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"); } }
Attempts:
2 left
💡 Hint
Positional arguments must come before named arguments.
✗ Incorrect
The first argument is positional and assigned to 'first'. The named arguments assign 'second' and 'third' explicitly, so the output is 'One - Two - Three'.
🔧 Debug
advanced2: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); } }
Attempts:
2 left
💡 Hint
In C#, positional arguments must come before named arguments.
✗ Incorrect
The code tries to pass a positional argument after a named argument, which is not allowed and causes a compile-time error.
🧠 Conceptual
advanced2: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? } }
Attempts:
2 left
💡 Hint
Named arguments do not change the types expected by the overloads.
✗ Incorrect
Only Bar(x: 5, y: "hello") matches the overload with int and string parameters. The others cause ambiguity or type mismatch.
❓ Predict Output
expert2: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"); } }
Attempts:
2 left
💡 Hint
Optional parameters use default values if not provided.
✗ Incorrect
The call provides 'name' and 'city' by name. 'age' is omitted, so default 20 is used.