229k views
4 votes
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] = j + 1;

if (j > 2)
alpha[j - 1] = alpha[j] + 2;
}

1. alpha = {1, 2, 3, 4, 5}
2. alpha = {4, 5, 6, 7, 9}
3. alpha = {1, 5, 6, 7, 5}
4. None of these

1 Answer

5 votes

Answer:

3. alpha = {1, 5, 6, 7, 5}

Step-by-step explanation:

Initially, we have that:

alpha[0] = 1;

alpha[1] = 2;

alpha[2] = 3;

alpha[3] = 4;

alpha[4] = 5;

For j higher than 2, we have that:

alpha[j - 1] = alpha[j] + 2;

So

j = 3

a[2] = alpha[3]+2 = 4 + 2 = 6;

j = 4

a[3] = alpha[4]+2 = 5+2 = 7;

The correct answer is:

3. alpha = {1, 5, 6, 7, 5}

User Quartermeister
by
6.5k points