Last Updated : 10 Apr, 2024

Given an array arr[] = {2, 9, 8, 4, 6} we need to delete an element 8 from it. After which we will get new array. Given below is the code to perform deletion operation. After deleting the element 8, array is printed.

int arr[] {2, 9, 8, 4, 6};
int i;
for(i = 0; i < sizeof(arr)/sizeof(arr[0]); i++)
    {
        if(arr[i] == 8)
          break;
    }
    // cout << i << endl;
for(int j = i; j < sizeof(arr)/sizeof(arr[0]); j++)
    {
        arr[j] = arr[j+1];
    }    
for(int k = 0; k < sizeof(arr)/sizeof(arr[0]); k++)
     cout << arr[k] << \" \"; 

Pick the correct option for above task to occur.
(A) Search for 8, left shift array elements, 2 9 4 6 6
(B) Search for 8, left shift array elements, 2 9 4 6 garbage value
(C) arr[4] = 0 and search for 8, left shift array elements, 2 9 4 6 0
(D) None of the mentioned.


Answer: (B)

Explanation:
First search for 8, get the index of that and left shift array elements. The new array will be 2 9 4 6 garbage value


Share your thoughts in the comments