ProgrammingHardAmazon/Microsoft SDE MCQ · Java placement

What is printed?

What is printed?
System.out.print(Math.abs(Integer.MIN_VALUE) + " ");
System.out.print(Integer.MAX_VALUE + 1 == Integer.MIN_VALUE);
  1. A. -2147483648 true
  2. B. 2147483647 true
  3. C. 2147483647 false
  4. D. -2147483648 false

Correct answer: A. -2147483648 true

+2147483648 does not fit in an int, so negating MIN_VALUE overflows back to MIN_VALUE and Math.abs returns a negative number. int arithmetic wraps silently, so MAX_VALUE + 1 equals MIN_VALUE.

More OOP with C++ and Java questions

Exams that ask this topic