C. Loop direction is wrong; should shift elements left, not right.
D. The loop should start from 0 and go to arr.Length - 2.
Solution
Step 1: Analyze loop direction
The loop copies arr[i-1] to arr[i], shifting elements right, which is incorrect for deletion.
Step 2: Correct loop to shift elements left
Loop should assign arr[i] = arr[i+1] starting from i=0 to length-2.
Final Answer:
Loop direction is wrong; should shift elements left, not right. -> Option C
Quick Check:
Shift left to delete first element [OK]
Hint: Shift elements left, not right, when deleting at start [OK]
Common Mistakes:
Shifting elements in wrong direction
Trying to reduce array size directly
Ignoring last element cleanup
5. You have an array int[] arr = {3, 6, 9, 12, 15}. Write a method to delete the first element and return a new array with the remaining elements only (size reduced by one). Which approach is correct?
hard
A. Use arr.RemoveAt(0) to delete the first element.
B. Shift elements left in original array and keep the same size.
C. Set arr[0] = 0 and return the original array.
D. Create a new array of size arr.Length - 1 and copy elements from index 1 onward.
Solution
Step 1: Understand fixed size arrays
Arrays in C# have fixed size; to reduce size, create a new array.
Step 2: Copy elements except first
Create new array of size one less and copy elements from index 1 onward.
Final Answer:
Create a new array of size arr.Length - 1 and copy elements from index 1 onward. -> Option D
Quick Check:
New smaller array with elements from index 1 [OK]
Hint: Create new smaller array and copy from index 1 [OK]
Common Mistakes:
Trying to resize original array directly
Only shifting elements without resizing
Using non-existent RemoveAt method on array
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.