Question 1
What does the following program print to standard output? ```java public class Main { public static void main(String[] args) { System.out.println(Math.round(2.5) + Math.round(-2.5)); } } ```
A. 0
Assumes Math.round uses symmetric rounding: half-away-from-zero gives 3 + (−3) = 0, and half-to-even (banker's rounding) gives 2 + (−2) = 0. Java's Math.round always rounds halves toward positive infinity, which is asymmetric around the midpoint.
B. `1.0`
Assumes Math.round(double) returns a double; it actually returns long. System.out.println(1L) prints '1', not '1.0'.
C. 1Correct answer
Math.round(double) is specified as (long)Math.floor(a + 0.5d). For 2.5: floor(3.0) = 3L; for −2.5: floor(−2.0) = −2L. The sum 3 + (−2) = 1 is printed as the long value 1 (Javadoc: Math.round(double)).
D. `-1`
Results from applying Math.floor directly without the +0.5 offset: floor(2.5) = 2 and floor(−2.5) = −3, summing to −1. Math.round adds 0.5 before flooring, which shifts the midpoint result upward.
Explanation
Java's Math.round(double) is defined as (long)Math.floor(a + 0.5d), which rounds ties toward positive infinity — not symmetrically. For 2.5 the formula yields floor(3.0) = 3L, and for −2.5 it yields floor(−2.0) = −2L, so the sum is 1. Candidates who expect symmetric half-away-from-zero rounding (3 + (−3) = 0) or half-to-even/banker's rounding (2 + (−2) = 0) arrive at 0; confusing Math.round with a bare Math.floor (floor(2.5) = 2, floor(−2.5) = −3, sum = −1) is another trap. Because Math.round(double) returns long rather than double, the result prints as '1', not '1.0'.