Given the code fragment:

float x = 22.00f % 3.00f;
int y = 22 % 3;
System.out.print(x + ", " + y);

What is the result?

A.

1.0, 1

B.

1.0f, 1

C.

7.33, 7

D. Compilation fails
E. An exception is thrown at runtime

題解

「%」是餘數運算子,支援整數和單倍精準浮點數(float)型態的餘數計算。若運算元的其中一個為單倍精準浮點數型態,則計算出來的結果也會是單倍精準浮點數型態。

22 除以 3 的結果為 7 餘 1,因此第 6 行會算出單倍精準浮點數型態的「1」,而第 7 行會算出整數型態的「1」。