Given the code fragment:
int a = -10;
int b = 17;
int c = expression1;
int d = expression2;
c++;
d--;
System.out.print(c + ", " + d);
What could expression1 and expression2 be, respectively, in order to produce output –8, 16?
A.
++a, --b
B.
++a, b--
C.
a++, --b
D.
a++, b--
题解
这题可以先往回推算至第 12 行运行完毕,最后 c = -8 - 1 = -9,d = 16 + 1 = 17。
所以第 12 行,要直接把 b 的值指派给 d,因此选项 A 和选项 C 都不对。第 11 行,要把 a 的值加一然后指派给 c,因此选项 C 和选项 D 都不对。所以答案是选项 B。
