56.8k views
5 votes
What is the value of alpha[2] after the following code executes?

int alpha[5];
int j;
for (j = 0; j < 5; j++)
alpha[j] = 2 * j + 1;

1 Answer

2 votes

Final answer:

The value of alpha[2] after the code executes is 5.

Step-by-step explanation:

In the given code, the array alpha of size 5 is initialized. The for loop iterates from j = 0 to j = 4 (5 times), where each alpha[j] is assigned a value computed as 2 * j + 1. When j = 2, the computation gives 2 * 2 + 1, resulting in alpha[2] being assigned the value 5.

The loop starts at j = 0, assigning alpha[0] = 1, then alpha[1] = 3, alpha[2] = 5, alpha[3] = 7, and alpha[4] = 9.

So, alpha[2] holds the value 5 at the end of the loop.

The correct answer is: The value of alpha[2] after the code executes is 5.

User Nonsensation
by
8.2k points