Advertisements
Advertisements
Question
What will be the output of the given Java code after the calculation [note the data types]?
double kv = Math.abs(Math.min(Math.floor(9.9), Math.ceil(9.9))−Math.round(24.6));
System.out.println ("KV=“ +kv);
Advertisements
Solution
Math.floor(9.9): Rounds downward to the nearest mathematical integer, resulting in 9.0.
Math.ceil(9.9): Rounds upward to the nearest mathematical integer, resulting in 10.0.
Math.min(9.0, 10.0): Compares the two values and returns the smaller one, which is 9.0.
Math.round(24.6): Rounds to the closest integer value, which results in 25 (returned as a long).
(9.0 − 25): The long value 25 is implicitly promoted to a double (25.0) for the subtraction. Thus, 9.0 − 25.0 evaluates to −16.0.
Math.abs(−16.0): Returns the absolute (positive) value of the argument, which is 16.0. This final result is stored in the double variable kv.
