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

Array bounds checking behavior 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 an array of 5 integers.

C Sharp (C#)
int[] numbers = new int[[1]];
Drag options to blanks, or click blank then click option'
A5
B0
C10
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using zero or negative numbers for array size causes errors.
2fill in blank
medium

Complete the code to access the first element of the array.

C Sharp (C#)
int first = numbers[[1]];
Drag options to blanks, or click blank then click option'
A0
B1
C5
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 as the first index instead of 0.
3fill in blank
hard

Fix the error in the code to avoid an IndexOutOfRangeException.

C Sharp (C#)
int value = numbers[[1]];
Drag options to blanks, or click blank then click option'
A5
B4
C6
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using the array length as an index, which is out of bounds.
4fill in blank
hard

Fill both blanks to create a loop that safely accesses all elements of the array.

C Sharp (C#)
for (int i = [1]; i [2] numbers.Length; i++) {
    Console.WriteLine(numbers[i]);
}
Drag options to blanks, or click blank then click option'
A0
B<=
C<
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= causes the loop to access an invalid index equal to the array length.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each number to its square if the number is less than 5.

C Sharp (C#)
var squares = numbers.Where(n => n [1] 5).ToDictionary(n => n, n => n [2] [3]);
Drag options to blanks, or click blank then click option'
A<
B*
Cn
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= instead of < changes the filter condition.
Using + instead of * does not compute the square.