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。