Problem of the Day
Tuesday, October 6, 2026
Problem:
In Java, multiplying the double value for one million times itself yields the following correct result:
double val = 1000000.0;
System.out.println(val * val);
--> produces the double result 1.0E12
Multiplying the int value for one million times itself yields something different:
int val2 = 1000000;
System.out.println(val2 * val2);
--> produces the int result -727379968
Which of the following explains the second result?
- The result is due to decimal-to-binary rounding error
- The result is due to an
intoverflow error - The result is due to a syntax error in the Java code
- The result is due to big-number imprecision in the CPU
The correct answer is b. Although 1000000 will fit into an int variable, the int result of the multiplication is well above the maximum value that will fit into an integer variable, 2147483647 (which you can observe by printing the value of Integer.MAX_INT).