Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a new element at the end of the array.
DSA C#
int[] arr = new int[5] {1, 2, 3, 4, 0}; arr[[1]] = 5; Console.WriteLine(string.Join(", ", arr));
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 5 which is out of bounds.
Using index 0 which overwrites the first element.
✗ Incorrect
The last valid index for a 5-element array is 4, so we assign the new value at index 4.
2fill in blank
mediumComplete the code to increase the size of the array and insert a new element at the end.
DSA C#
int[] arr = new int[3] {1, 2, 3}; int[] newArr = new int[arr.Length + 1]; for (int i = 0; i < arr.Length; i++) { newArr[i] = arr[i]; } newArr[[1]] = 4; arr = newArr; Console.WriteLine(string.Join(", ", arr));
Attempts:
3 left
💡 Hint
Common Mistakes
Using an index equal to newArr.Length which is out of bounds.
Using index 0 which overwrites the first element.
✗ Incorrect
The new element should be inserted at the last index, which is the old length (3).
3fill in blank
hardFix the error in the code to correctly insert an element at the end of the array.
DSA C#
int[] arr = new int[4] {1, 2, 3, 0}; arr[[1]] = 4; Console.WriteLine(string.Join(", ", arr));
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 4 which is out of bounds.
Using negative index.
✗ Incorrect
Index 3 is the last valid index for a 4-element array, so the new value should be assigned there.
4fill in blank
hardFill both blanks to create a new array with one extra space and insert a new element at the end.
DSA C#
int[] arr = new int[2] {1, 2}; int[] newArr = new int[arr.[1] + 1]; for (int i = 0; i < arr.[2]; i++) { newArr[i] = arr[i]; } newArr[2] = 3; arr = newArr; Console.WriteLine(string.Join(", ", arr));
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Count' which is not valid for arrays.
Using 'Size' or 'Capacity' which do not exist for arrays.
✗ Incorrect
The property 'Length' gives the number of elements in the array, used to create new array and loop through elements.
5fill in blank
hardFill all three blanks to copy elements, insert a new element at the end, and update the original array.
DSA C#
int[] arr = new int[3] {10, 20, 30}; int[] newArr = new int[arr.[1] + 1]; for (int [2] = 0; [2] < arr.Length; [2]++) { newArr[[2]] = arr[[2]]; } newArr[arr.Length] = 40; arr = newArr; Console.WriteLine(string.Join(", ", arr));
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count' which is invalid for arrays.
Using different variable names inconsistently.
✗ Incorrect
Use 'Length' for array size, and 'i' as the loop variable to copy elements correctly.
