Ask Question

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] pos; j--) a[j] = a[j - 1]; a[pos] = value; size++;

+2
Answers (1)
  1. 23 July, 04:38
    0
    a={5,1, 3,7, 0, 0}

    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.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “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 ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers