1Z0-811 · Question #62
Given the code fragment: public static void main (String[] args) { double num = -25.67; System.out.println (Math.abs (num)); } What is the result?
The correct answer is A. 25.67. Math.abs() returns the absolute value of a number - it simply strips the negative sign, leaving the magnitude intact. So Math.abs(-25.67) returns exactly 25.67, making A correct. Why the distractors are wrong: B (25.00) - Math.abs does not truncate or round; it preserves the…
Question
Options
- A25.67
- B25.00
- C25.7
- D26
How the community answered
(34 responses)- A94% (32)
- B3% (1)
- D3% (1)
Explanation
Math.abs() returns the absolute value of a number - it simply strips the negative sign, leaving the magnitude intact. So Math.abs(-25.67) returns exactly 25.67, making A correct.
Why the distractors are wrong:
- B (25.00) -
Math.absdoes not truncate or round; it preserves the full decimal value. - C (25.7) - No rounding occurs; the fractional part
.67is unchanged. - D (26) -
Math.absdoes not round up to the nearest integer; that would requireMath.ceilorMath.round.
Memory tip: Think of Math.abs as "absolute value from math class" - it only changes the sign, never the digits. If you see rounding in the choices, eliminate them immediately.
Topics
Community Discussion
No community discussion yet for this question.