Multiple choice technology programming languages

class D { static boolean m(double v) { return(v != v == Double.isNaN(v)); } public static void main (String args[]) { double d1 = Double.NaN; double d2 = Double.POSITIVE_INFINITY; double d3 = Double.MAX_VALUE; System.out.print(m(d1) + "," + m(d2) + "," + m(d3)); }} What is the result of attempting to compile and run the program?

  1. Prints: false,false,false

  2. Prints: false,true,false

  3. Prints: true,false,false

  4. Prints: true,true,false

  5. Prints: true,true,true

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

In Java, the expression v != v evaluates to true if and only if v is Double.NaN (since NaN is not equal to itself). Thus, v != v is logically equivalent to Double.isNaN(v). Comparing them with == always yields true for any double value, printing 'true,true,true'.

AI explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) Prints: false,false,false - This option is incorrect because it does not match the expected output.

Option B) Prints: false,true,false - This option is incorrect because it does not match the expected output.

Option C) Prints: true,false,false - This option is incorrect because it does not match the expected output.

Option D) Prints: true,true,false - This option is incorrect because it does not match the expected output.

Option E) Prints: true,true,true - This option is correct because it matches the expected output.

The correct answer is Option E. This option is correct because the method m(double v) returns true when the input is Double.NaN (not-a-number) and true when the input is Double.POSITIVE_INFINITY (positive infinity). Therefore, when d1 is Double.NaN, m(d1) returns true. Similarly, when d2 is Double.POSITIVE_INFINITY, m(d2) returns true. However, when d3 is Double.MAX_VALUE, m(d3) returns false because Double.MAX_VALUE is a finite value and not equal to Double.NaN or Double.POSITIVE_INFINITY.

Thus, the program will print: true,true,true.