ProgrammingModerateTCS NQT Prime · Infosys SP

What is printed?

What is printed?
int x = Integer.MAX_VALUE;
long y = x + 1;
long z = x + 1L;
System.out.print(y + " " + z);
  1. A. -2147483648 2147483648
  2. B. 2147483648 2147483648
  3. C. -2147483648 -2147483648
  4. D. 2147483648 -2147483648

Correct answer: A. -2147483648 2147483648

x + 1 is evaluated in int and overflows before widening; x + 1L is evaluated in long.

More OOP with C++ and Java questions

Exams that ask this topic