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

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Parameter Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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");
    }
}
AHello\nHello
BHello
CHello\nHello\nHello
DCompilation error
Attempts:
2 left
💡 Hint
Check the default value of the parameter times.
🧠 Conceptual
intermediate
2:00remaining
Understanding ref and out parameters
Which statement about ref and out parameters in C# is true?
A<code>out</code> parameters must be initialized before passing; <code>ref</code> parameters do not need to be initialized.
B<code>ref</code> parameters cannot be modified inside the method.
CBoth <code>ref</code> and <code>out</code> parameters must be initialized before passing.
D<code>ref</code> parameters must be initialized before passing; <code>out</code> parameters do not need to be initialized before passing but must be assigned inside the method.
Attempts:
2 left
💡 Hint
Think about whether the variable needs a value before calling the method.
🔧 Debug
advanced
2: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);
    }
}
ANo error; outputs 6, 15, 0, 7
BCompilation error: params must be last parameter
CRuntime error: NullReferenceException
DCompilation error: params cannot be used with arrays
Attempts:
2 left
💡 Hint
Check how the params keyword works with arrays and multiple arguments.
Predict Output
advanced
2: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);
    }
}
ACompilation error: missing ref keyword
B10
C15
DRuntime error
Attempts:
2 left
💡 Hint
Remember that ref passes the variable by reference, so changes affect the original.
Predict Output
expert
2: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);
    }
}
ARuntime error
BCompilation error: named arguments must come after positional arguments
CNumbers:\n2 3
DNumbers:\n1 2 3
Attempts:
2 left
💡 Hint
Check the order of named and positional arguments in method calls.