Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to remove the first element from the array by shifting elements left.
DSA Java
for (int i = 0; i < n - 1; i++) { arr[i] = arr[[1]]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using arr[i - 1] causes index errors.
Using arr[i] does not shift elements.
Using arr[n - i] accesses out of bounds.
✗ Incorrect
To delete the first element, each element is replaced by the next element, so arr[i] = arr[i + 1].
2fill in blank
mediumComplete the code to decrease the size of the array after deletion.
DSA Java
n = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Increasing n instead of decreasing.
Setting n to zero resets the array incorrectly.
✗ Incorrect
After deleting one element, the size n should decrease by 1.
3fill in blank
hardFix the error in the loop condition to avoid out-of-bounds access during deletion.
DSA Java
for (int i = 0; i < [1]; i++) { arr[i] = arr[i + 1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using n causes index out of bounds.
Using n + 1 causes runtime errors.
✗ Incorrect
The loop should run till n - 1 to avoid accessing arr[n], which is out of bounds.
4fill in blank
hardFill both blanks to print the array elements after deletion.
DSA Java
for (int i = 0; i < [1]; i++) { System.out.print(arr[[2]] + " "); }
Attempts:
3 left
💡 Hint
Common Mistakes
Looping till n causes printing of invalid elements.
Using arr[i + 1] skips the first element.
✗ Incorrect
After deletion, print elements from index 0 to n-2 using arr[i].
5fill in blank
hardFill all three blanks to implement a method that deletes the first element from an array.
DSA Java
public static int deleteFirst(int[] arr, int n) {
for (int i = 0; i < [1]; i++) {
arr[i] = arr[[2]];
}
n = [3];
return n;
}Attempts:
3 left
💡 Hint
Common Mistakes
Looping till n causes index errors.
Incorrectly updating n to n + 1 instead of n - 1.
✗ Incorrect
Loop runs till n-1, shifting elements left by assigning arr[i] = arr[i+1], then size decreases by 1.
