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

Params keyword for variable arguments in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a method that accepts a variable number of integer arguments using the correct keyword.

C Sharp (C#)
public void PrintNumbers([1] int[] numbers) {
    foreach (int num in numbers) {
        Console.WriteLine(num);
    }
}
Drag options to blanks, or click blank then click option'
Aparams
Bargs
Cvariable
Dvarargs
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'varargs' which is not a C# keyword.
Forgetting to use any keyword and just writing 'int[] numbers'.
2fill in blank
medium

Complete the method call to pass multiple integers to the method using the params keyword.

C Sharp (C#)
PrintNumbers([1]);
Drag options to blanks, or click blank then click option'
A1, 2, 3
Bnew int[] {1, 2, 3}
C{1, 2, 3}
Dint[] {1, 2, 3}
Attempts:
3 left
💡 Hint
Common Mistakes
Passing an array explicitly when not needed.
Using curly braces without 'new int[]'.
3fill in blank
hard

Fix the error in the method declaration to correctly use the params keyword for variable arguments.

C Sharp (C#)
public void SumNumbers([1] int[] numbers) {
    int sum = 0;
    foreach (int num in numbers) {
        sum += num;
    }
    Console.WriteLine(sum);
}
Drag options to blanks, or click blank then click option'
Aout
Bparams
Cref
Din
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ref' or 'out' instead of 'params'.
Placing the keyword after the parameter name.
4fill in blank
hard

Fill both blanks to declare a method that sums variable integer arguments and returns the total.

C Sharp (C#)
public int SumAll([1] int[] numbers) {
    int total = 0;
    foreach (int num in [2]) {
        total += num;
    }
    return total;
}
Drag options to blanks, or click blank then click option'
Aparams
Bnumbers
Cnums
Dargs
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name in the loop than the parameter name.
Forgetting the 'params' keyword.
5fill in blank
hard

Fill all three blanks to declare a method that prints each string argument passed using the params keyword.

C Sharp (C#)
public void PrintAll([1] string[] [2]) {
    foreach (string [3] in [2]) {
        Console.WriteLine([3]);
    }
}
Drag options to blanks, or click blank then click option'
Aparams
Bwords
Cword
Dargs
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names between parameter and loop.
Omitting the 'params' keyword.