フリー問題

Java SE 21 Developer Professional のフリー問題 1 / 20 問目

問題文

A developer writes the single line int average = (7 + 8) / 2; and expects the variable to hold 7.5. What value does average actually hold, and why?

選択肢

  1. It holds 8, because integer division rounds its result to the nearest whole number, and the exact quotient 7.5 lies on the midpoint between 7 and 8.
  2. The line does not compile, because the result of the division is a double and a double cannot be assigned to an int without an explicit cast.
  3. It holds 7, and the compiler evaluates the addition after the division and then truncates the intermediate double value that it produced.
  4. It holds 7, because division of two int operands is integer division and the fractional part is discarded rather than rounded.

解答・解説を確認するには

正解と解説の確認、回答の記録には無料登録が必要です。登録すると演習モードでフリー問題に回答し、正誤と解説をその場で確認できます。