Multiple choice technology programming languages

What is the result of trying to compile and run the following code. public final static void main(String[] args){ double d = 10.0 / -0; if(d == Double.POSITIVE_INFINITY) System.out.println("Positive infinity"); else System.out.println("Negative infinity"); }

  1. output Positive infinity

  2. output Negative infinity

  3. Will fail to compile

  4. Runtime exception

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Dividing a positive double by negative zero in Java yields negative infinity (-0.0 or negative infinity representation). Therefore, the comparison d == Double.POSITIVE_INFINITY evaluates to false, and the code prints Negative infinity.

AI explanation

The expression '-0' as an int literal is unary minus applied to 0, which is just the int value 0 — Java's int type has no signed zero. When 10.0 (a double) is divided by that int 0, the 0 is promoted to double 0.0 (not negative zero), so the division 10.0/0.0 evaluates to Double.POSITIVE_INFINITY under IEEE 754 floating-point rules (division of a positive number by zero yields positive infinity), and no exception is thrown for floating-point division by zero. The if-condition d == Double.POSITIVE_INFINITY is therefore true, so the code compiles, runs, and prints 'Positive infinity'. It would only print negative infinity if the numerator were negative or if -0 were an actual signed double zero.