問題文
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?
選択肢
- 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.
- 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.
- It holds 7, and the compiler evaluates the addition after the division and then truncates the intermediate double value that it produced.
- It holds 7, because division of two int operands is integer division and the fractional part is discarded rather than rounded.