181k views
5 votes
Given the following code:

int i = 100;
int j = 10;
while (i > 0)
{
i = i / j;
j = 1 + j % 5;
}
What is the value of j after this code executes?

1 Answer

4 votes
Let's go through step by step, starting from inside the while loop.


Is i > 0? Yes. Continue.

ITERATION 1:
i now equals 10 (100 / 10)
j now equals 1 (1 + 10 % 5)

Is i > 0? Yes. Continue.

ITERATION 2:
i now equals 10 (10 / 1)
j now equals 0 (1 + 1 % 5)

Is i > 0? Yes. Continue.

ITERATION 3:
i = i / j; now generates a DivideByZeroException, thus we get a run-time error.
User GiorgosK
by
8.4k points