What are the values of i, j, and k after the following code statements?

int i = 2;

int j = 3;

int k = 4;

int x = i + j + k;

i = x - i - j;

j = x - j - k;

k = x - i - k;

+7 votes

Rather than creating one big equation to define “k” at the end I suggest that you just do the computation in order, just as the computer would. So it would be “x” which, when it is defined, is 9, minus “i” which was 2 but has been redefined to be “x-i-j”, so when the final “k” is computed, “i” is 4, and then minus “k” which during the final defining of “k” it is 4. Therefore, the output of “k” is (9 - 4 - 4 = 1).

When in doubt, try to think through it like the computer would.

Hope this cleared it up.