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

Array indexing and access 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 access the first element of the array.

C Sharp (C#)
int[] numbers = {1, 2, 3, 4};
int first = numbers[[1]];
Drag options to blanks, or click blank then click option'
A0
B1
C4
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 as the first index instead of 0.
Using a negative index which is invalid.
2fill in blank
medium

Complete the code to access the last element of the array using its length.

C Sharp (C#)
int[] values = {10, 20, 30, 40, 50};
int last = values[values.Length [1] 1];
Drag options to blanks, or click blank then click option'
A+
B/
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using +1 which causes an index out of range error.
Using multiplication or division instead of subtraction.
3fill in blank
hard

Fix the error in accessing the third element of the array.

C Sharp (C#)
string[] fruits = {"apple", "banana", "cherry"};
string third = fruits[[1]];
Drag options to blanks, or click blank then click option'
A3
B2
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 3 as index which is out of range.
Using 1 or 0 which points to earlier elements.
4fill in blank
hard

Fill both blanks to create an array of squares for numbers 1 to 5.

C Sharp (C#)
int[] squares = new int[5];
for (int i = 0; i [1] 4; i++) {
    squares[i] = (i [2] 1) * (i [2] 1);
}
Drag options to blanks, or click blank then click option'
A<=
B<
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of <= causes missing the last element.
Subtracting 1 instead of adding 1 changes the numbers incorrectly.
5fill in blank
hard

Fill all three blanks to create a reversed copy of the array.

C Sharp (C#)
int[] original = {1, 2, 3, 4};
int[] reversed = new int[original.Length];
for (int [1] = 0; [1] < original.Length; [1]++) {
    reversed[[2]] = original[original.Length [3] 1 - [1]];
}
Drag options to blanks, or click blank then click option'
Ai
Boriginal.Length - 1 - i
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong loop variable names inconsistently.
Incorrect calculation of reversed index causing errors.