Bird
Raised Fist0
C Sharp (C#)programming~20 mins

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

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

Practice

(1/5)
1. What is the role of a parameter in a C# method?
easy
A. It acts as a placeholder for values passed to the method.
B. It stores the result returned by the method.
C. It defines the method's name.
D. It calls another method inside the current method.

Solution

  1. Step 1: Understand method definition

    Parameters are declared in the method signature to receive values.
  2. Step 2: Differentiate parameters from other parts

    Parameters are not method names or return values but placeholders for inputs.
  3. Final Answer:

    It acts as a placeholder for values passed to the method. -> Option A
  4. Quick Check:

    Parameter = placeholder [OK]
Hint: Parameters hold input spots in methods [OK]
Common Mistakes:
  • Confusing parameters with return values
  • Thinking parameters are method names
  • Mixing parameters with method calls
2. Which of the following is the correct way to define a method with one integer parameter named number in C#?
easy
A. void PrintNumber(number int) { }
B. void PrintNumber(int) { }
C. void PrintNumber(int number) { }
D. void PrintNumber(number) { }

Solution

  1. Step 1: Recall C# parameter syntax

    Parameters require type first, then name: int number.
  2. Step 2: Check each option

    void PrintNumber(int number) { } matches correct syntax; others have wrong order or missing names.
  3. Final Answer:

    void PrintNumber(int number) { } -> Option C
  4. Quick Check:

    Type before name in parameters [OK]
Hint: Parameter syntax: type then name [OK]
Common Mistakes:
  • Swapping parameter type and name
  • Omitting parameter name
  • Using parentheses without parameter types
3. What will be the output of this C# code?
void Greet(string name) {
    Console.WriteLine($"Hello, {name}!");
}

Greet("Alice");
medium
A. Hello, Alice!
B. Hello, name!
C. Hello, !
D. Error: Missing argument

Solution

  1. Step 1: Identify the parameter and argument

    The method Greet has a parameter name and is called with argument "Alice".
  2. Step 2: Understand string interpolation output

    The output will replace {name} with "Alice" in the printed string.
  3. Final Answer:

    Hello, Alice! -> Option A
  4. Quick Check:

    Argument replaces parameter in output [OK]
Hint: Arguments fill parameters when calling methods [OK]
Common Mistakes:
  • Printing parameter name literally
  • Forgetting to pass argument
  • Confusing parameter with variable
4. What is wrong with this method call?
void PrintSum(int a, int b) {
    Console.WriteLine(a + b);
}

PrintSum(5);
medium
A. Too many arguments passed.
B. Too few arguments passed.
C. Parameter types do not match arguments.
D. Method name is incorrect.

Solution

  1. Step 1: Check method parameters

    The method PrintSum requires two integer parameters: a and b.
  2. Step 2: Check method call arguments

    The call PrintSum(5); provides only one argument instead of two.
  3. Final Answer:

    Too few arguments passed. -> Option B
  4. Quick Check:

    Parameter count must match argument count [OK]
Hint: Count arguments to match parameters [OK]
Common Mistakes:
  • Passing fewer arguments than parameters
  • Ignoring method signature requirements
  • Assuming default values without specifying
5. You want to create a method CalculateArea that takes two parameters width and height and returns their product. Which method signature and call correctly use parameters and arguments?
hard
A. Method: int CalculateArea(int width, int height)
Call: CalculateArea(5);
B. Method: int CalculateArea(width, height)
Call: CalculateArea(5, 10);
C. Method: void CalculateArea(int width, int height)
Call: CalculateArea(5, 10);
D. Method: int CalculateArea(int width, int height)
Call: CalculateArea(5, 10);

Solution

  1. Step 1: Check method signature correctness

    Method: int CalculateArea(int width, int height)
    Call: CalculateArea(5, 10); correctly declares parameter types and names and returns int.
  2. Step 2: Check method call arguments

    Method: int CalculateArea(int width, int height)
    Call: CalculateArea(5, 10); calls method with two integer arguments matching parameters.
  3. Step 3: Analyze other options

    Method: int CalculateArea(width, height)
    Call: CalculateArea(5, 10); misses types in parameters; Method: int CalculateArea(int width, int height)
    Call: CalculateArea(5); passes only one argument; Method: void CalculateArea(int width, int height)
    Call: CalculateArea(5, 10); returns void, so no product returned.
  4. Final Answer:

    Method with int parameters and int return, called with two arguments. -> Option D
  5. Quick Check:

    Signature and call must match parameters and arguments [OK]
Hint: Match parameter types and argument count exactly [OK]
Common Mistakes:
  • Omitting parameter types
  • Passing wrong number of arguments
  • Using void return when value needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard