Question discussion

What is the output of the following Java program?public class Main { public static void main(String[] args) { int x = Integer.MAX_VALUE; x+…

1912Medium 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) {
        int x = Integer.MAX_VALUE;
        x++;
        System.out.println(x + " " + (x < 0));
    }
}
  • A2147483648 false
  • B-2147483647 true
  • C-2147483648 true
  • DArithmeticException is thrown
Correct answer: C

Explanation

Integer arithmetic wraps around silently on overflow, so MAX_VALUE + 1 becomes MIN_VALUE (-2147483648), which is negative.

Discussion 0

Nobody has replied yet. Start the discussion!