227k views
0 votes
What does a contain after the following code runs?int[] a = {1, 3, 7, 0, 0, 0};int size = 3, capacity = 6, value = 5;int pos = 0;for (pos = 0; pos < size; pos++)if (a[pos] < value) break;for (int j = size; j > pos; j--)a[j] = a[j - 1];a[pos] = value;size++;

User Wazner
by
6.3k points

1 Answer

4 votes

Answer:

a={5,1, 3,7, 0, 0}

Step-by-step explanation:

when the code runs,

a={1, 3, 7, 0, 0, 0} , size=3, capacity=6 ,value=5,pos=0.

When loop starts pos is zero for pos 0,1 if conditon is true and loop breaks and moves to next loop.

On first iteration pos is 0 and j is 3. it moves element at 3rd index to 2.

On second iteration pos is 0 and j is 2. it moves element at 2nd index to 1.

On third iteration pos is 0 and j is 1. it moves element at 1st index to 0.

On fourth iteration pos is 0 and j is 0.Hence the condition for loop is false and the code inside does not run. This behavior is followed until loop completes all iterations

As pos is not changing after each iteration 0th index is 5.

User Denny Lee
by
6.6k points