nerdexam
Oracle

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…

Data Types and Operators

Question

Given the code fragment: public static void main (String[] args) { double num = -25.67; System.out.println (Math.abs (num)); } What is the result?

Options

  • A25.67
  • B25.00
  • C25.7
  • D26

How the community answered

(34 responses)
  • A
    94% (32)
  • B
    3% (1)
  • D
    3% (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.abs does not truncate or round; it preserves the full decimal value.
  • C (25.7) - No rounding occurs; the fractional part .67 is unchanged.
  • D (26) - Math.abs does not round up to the nearest integer; that would require Math.ceil or Math.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

#Math.abs()#Absolute value#Math class#Double precision

Community Discussion

No community discussion yet for this question.

Full 1Z0-811 Practice