Type Casting and Truncation
Consider the following code segment:
double value = -7.8;
int rounded1 = (int)(value + 0.5);
int rounded2 = (int)(value - 0.5);
System.out.println(rounded1 + " " + rounded2);
What is the output of this code, and what does it demonstrate about the behavior of casting from double to int?
A
-7 -7, indicating that both expressions produce the same result
B
-7 -8, demonstrating that casting always truncates toward zero rather than rounding
C
-8 -8, proving that casting rounds negative numbers down
D
-8 -7, showing that the rounding formulas work correctly for negative numbers
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE