Question discussion

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

954Medium 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 i = Integer.MAX_VALUE; i++;
        char c = 'A'; c += 2;
        System.out.println(i + " " + c + " " + (c + 1));
    }
}
  • A2147483648 C D
  • B-2147483648 C 68
  • C-2147483648 C D
  • D2147483647 C 68
Correct answer: B

Explanation

Integer overflow wraps MAX_VALUE to −2147483648; c becomes 'C' and (c + 1) is an int expression, i.e. 68.

Discussion 0

Nobody has replied yet. Start the discussion!