Ask Question

What is stored in alpha after the following code executes?

int[] alpha = new int[5]; int j; for (j = 0; j < 5; j++) {alpha[j] = 2 * j; if (j % 2 = = 1) alpha[j - 1] = alpha[j] + j; }1. alpha = {0, 2, 4, 6, 8}

2. alpha = {0, 2, 9, 6, 8}

3. alpha = {3, 2, 9, 6, 8}

4. alpha = {0, 3, 4, 7, 8}

+1
Answers (1)
  1. 23 September, 07:20
    0
    3. alpha = {3, 2, 9, 6, 8}

    Explanation:

    Initially, we have that alpha[j] = 2*j;

    j goes from 0 to 4.

    So

    alpha[0] = 0;

    alpha[1] = 2;

    alpha[2] = 4;

    alpha[3] = 6;

    alpha[4] = 8;

    If (j%2 = = 1), which happens when j = 1 and j = 3, alpha[j-1] = alpha[j]+j;

    So

    j = 1

    alpha[1-1] = alpha[1]+1 = 2+1 = 3

    So alpha[0] = 3

    j = 3

    alpha[3-1] = alpha[3]+3 = 6+3 = 9

    So alpha[2] = 9

    The correct answer is:

    3. alpha = {3, 2, 9, 6, 8}
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “What is stored in alpha after the following code executes? int[] alpha = new int[5]; int j; for (j = 0; j < 5; j++) {alpha[j] = 2 * j; if ...” 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