Challenge - 5 Problems
Parameter Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of method with default and named arguments
What is the output of this C# code when calling
PrintMessage("Hello")?C Sharp (C#)
using System; class Program { static void PrintMessage(string message, int times = 2) { for (int i = 0; i < times; i++) { Console.WriteLine(message); } } static void Main() { PrintMessage("Hello"); } }
Attempts:
2 left
💡 Hint
Check the default value of the parameter
times.✗ Incorrect
The method
PrintMessage prints the message the number of times specified by times. Since times defaults to 2, it prints "Hello" twice.🧠 Conceptual
intermediate2:00remaining
Understanding ref and out parameters
Which statement about
ref and out parameters in C# is true?Attempts:
2 left
💡 Hint
Think about whether the variable needs a value before calling the method.
✗ Incorrect
ref parameters require the variable to be initialized before passing. out parameters do not require initialization before passing but must be assigned a value inside the method before returning.🔧 Debug
advanced2:00remaining
Identify the error with params keyword
What error will this code produce?
C Sharp (C#)
using System; class Program { static void Sum(params int[] numbers) { int total = 0; foreach (int n in numbers) { total += n; } Console.WriteLine(total); } static void Main() { Sum(1, 2, 3); Sum(new int[] {4, 5, 6}); Sum(); Sum(7); } }
Attempts:
2 left
💡 Hint
Check how the params keyword works with arrays and multiple arguments.
✗ Incorrect
The
params keyword allows passing zero or more arguments or an array. All calls are valid and print the sum correctly.❓ Predict Output
advanced2:00remaining
Output of method with ref parameter
What is the output of this code?
C Sharp (C#)
using System; class Program { static void Increment(ref int number) { number += 5; } static void Main() { int x = 10; Increment(ref x); Console.WriteLine(x); } }
Attempts:
2 left
💡 Hint
Remember that
ref passes the variable by reference, so changes affect the original.✗ Incorrect
The method adds 5 to the original variable
x because it is passed by reference.❓ Predict Output
expert2:00remaining
Output of method with params and named arguments
What is the output of this code?
C Sharp (C#)
using System; class Program { static void Display(string title, params int[] values) { Console.WriteLine(title); foreach (int v in values) { Console.Write(v + " "); } Console.WriteLine(); } static void Main() { Display(title: "Numbers:", values: 1, 2, 3); } }
Attempts:
2 left
💡 Hint
Check the order of named and positional arguments in method calls.
✗ Incorrect
In C#, named arguments must come after all positional arguments. Here,
values is named but followed by positional arguments, causing a compilation error.