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

Why arrays are needed in C Sharp (C#) - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use arrays instead of individual variables?

Imagine you want to store the scores of 5 players in a game. Why is using an array better than using 5 separate variables?

AArrays allow storing multiple values in one variable, making code easier to manage and loop through.
BArrays automatically sort the values for you without extra code.
CArrays use less memory than individual variables for the same data.
DArrays prevent any changes to the stored values once set.
Attempts:
2 left
💡 Hint

Think about how you would handle many similar pieces of data together.

Predict Output
intermediate
2:00remaining
What is the output of this C# array code?

Look at this code and choose the output it produces.

C Sharp (C#)
int[] numbers = {10, 20, 30, 40};
int sum = 0;
for (int i = 0; i < numbers.Length; i++)
{
    sum += numbers[i];
}
Console.WriteLine(sum);
A10
B100
C40
D0
Attempts:
2 left
💡 Hint

Think about what the loop does with each number in the array.

🔧 Debug
advanced
2:00remaining
Find the error in this array usage

What error does this code cause when run?

C Sharp (C#)
int[] values = new int[3];
values[0] = 5;
values[1] = 10;
values[2] = 15;
values[3] = 20;
Console.WriteLine(values[3]);
ASyntaxError
BNullReferenceException
CNo error, prints 20
DIndexOutOfRangeException
Attempts:
2 left
💡 Hint

Check the size of the array and the index used.

📝 Syntax
advanced
2:00remaining
Which option correctly declares and initializes an array?

Choose the correct way to declare and initialize an integer array with values 1, 2, and 3 in C#.

Aint[] arr = new int[] {1, 2, 3};
Bint arr = {1, 2, 3};
Cint[] arr = (1, 2, 3);
Dint arr[] = new int(3) {1, 2, 3};
Attempts:
2 left
💡 Hint

Remember the syntax for array initialization in C#.

🚀 Application
expert
2:00remaining
How many items are in the resulting array after this code?

What is the length of the array result after running this code?

C Sharp (C#)
int[] baseArray = {2, 4, 6};
int[] result = new int[baseArray.Length * 2];
for (int i = 0; i < baseArray.Length; i++)
{
    result[i] = baseArray[i];
    result[i + baseArray.Length] = baseArray[i] * 2;
}
A4
B3
C6
D2
Attempts:
2 left
💡 Hint

Look at how the size of result is set and how the loop fills it.