Challenge - 5 Problems
Optional Parameters Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of method with optional parameters
What is the output of this C# code when calling
Greet() without arguments?C Sharp (C#)
using System; class Program { static void Greet(string name = "Guest", int times = 1) { for (int i = 0; i < times; i++) { Console.WriteLine($"Hello, {name}!"); } } static void Main() { Greet(); } }
Attempts:
2 left
💡 Hint
Check the default values of the parameters in the method signature.
✗ Incorrect
The method Greet has two optional parameters: name with default "Guest" and times with default 1. Calling Greet() uses these defaults, so it prints "Hello, Guest!" once.
❓ Predict Output
intermediate2:00remaining
Output with named and optional parameters
What is the output of this code snippet?
C Sharp (C#)
using System; class Program { static void Display(string message = "Hi", int count = 2) { for (int i = 0; i < count; i++) { Console.WriteLine(message); } } static void Main() { Display(count: 3); } }
Attempts:
2 left
💡 Hint
Named parameters can be used to specify only some arguments.
✗ Incorrect
The call Display(count: 3) uses the default message "Hi" and overrides count to 3, so it prints "Hi" three times.
🔧 Debug
advanced2:00remaining
Identify the error with optional parameters
What error does this code produce when compiled?
C Sharp (C#)
class Program { static void Print(int x = 5, int y) { System.Console.WriteLine(x + y); } static void Main() { Print(3); } }
Attempts:
2 left
💡 Hint
Check the order of optional and required parameters in method signature.
✗ Incorrect
In C#, optional parameters must come after all required parameters. Here, x is optional but y is required and comes after x, causing CS1737.
❓ Predict Output
advanced2:00remaining
Output with multiple optional parameters and named arguments
What is the output of this program?
C Sharp (C#)
using System; class Program { static void Info(string name = "Unknown", int age = 0, string city = "Nowhere") { Console.WriteLine($"Name: {name}, Age: {age}, City: {city}"); } static void Main() { Info(city: "Paris", name: "Alice"); } }
Attempts:
2 left
💡 Hint
Named arguments can be passed in any order.
✗ Incorrect
The call uses named arguments city and name, so age uses default 0. Output shows name Alice, age 0, city Paris.
🧠 Conceptual
expert2:00remaining
Behavior of optional parameters with method overloading
Given these two methods, what happens when
Test(5) is called?C Sharp (C#)
class Program { static void Test(int x, int y = 10) { System.Console.WriteLine($"Method1: x={x}, y={y}"); } static void Test(int x) { System.Console.WriteLine($"Method2: x={x}"); } static void Main() { Test(5); } }
Attempts:
2 left
💡 Hint
Consider how C# resolves overloaded methods with optional parameters.
✗ Incorrect
C# prefers the method with exact parameter match over one with optional parameters. So Test(int x) is called, printing Method2.