Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the program?

public class Sample25
{
    public static void main(String[] args)
    {
        int biggest = Integer.MAX_VALUE;
        int wrapped = biggest + 1;
        System.out.println(wrapped);
    }
}

  • A.The program prints -2147483648
  • B.The program prints 2147483647
  • C.An ArithmeticException is thrown.
  • D.The program prints 2147483648

Answer: A

int arithmetic wraps silently on overflow rather than throwing or widening, so one past the largest int is the smallest one. The result is -2147483648. Adding 1L instead would promote the expression to long and give the mathematical answer.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.