Question discussion

What is the output of the following Java program?public class Main { public static void main(String[] args) { System.out.println(Math.round…

2331Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following Java program?

public class Main {
    public static void main(String[] args) {
        System.out.println(Math.round(2.5) + " " + Math.round(-2.5) + " " + Math.floor(-2.5));
    }
}
  • A3 -2 -2.0
  • B3 -2 -3.0
  • C2 -2 -2.0
  • D3 -3 -3.0
Correct answer: B

Explanation

Math.round(x) is floor(x + 0.5), which gives 3 for 2.5 and -2 for -2.5, while Math.floor(-2.5) is the largest integer not greater than it, -3.0.

Discussion 0

Nobody has replied yet. Start the discussion!