Ask Question

What is the value of alpha[3] after the following code executes? int[] alpha = new int[5]; int j; alpha[0] = 5; for (j = 1; j < 5; j++) {if (j % 2 = = 0) alpha[j] = alpha[j - 1] + 2; elsealpha[j] = alpha[j - 1] + 3; }1. None of these2. 133. 154. 10

+1
Answers (1)
  1. 6 May, 15:22
    0
    Option 2: 13

    Explanation:

    Given the code as follows:

    int[] alpha = new int[5]; int j; alpha[0] = 5; for (j = 1; j < 5; j++) { if (j % 2 = = 0) alpha[j] = alpha[j - 1] + 2; else alpha[j] = alpha[j - 1] + 3; }

    The alpha is an array with size of 5.

    The first element of the array has been initialized to 5 (Line 3).

    Within the for loop (4 - 10), there is if and else statements that check if the current index j (starting from 1 to 4) is an odd or even number.

    If j = 1, the else statement (Line 9) will run and set the alpha[1] to alpha[1-1] + 3 = 5 + 3 = 8

    If j = 2, the if statement (Line 7) will run and set the alpha[2] to alpha[2-1] + 2 = 8 + 2 = 10

    If j = 3, the else statement will run and set the alpha[3] to alpha[3-1] + 3 = 10 + 3 = 13
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “What is the value of alpha[3] after the following code executes? int[] alpha = new int[5]; int j; alpha[0] = 5; for (j = 1; j < 5; 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