Multiple choice technology programming languages

Given: 1. class Comp2 { 2. public static void main(String[] args) { 3. float f1 = 2.3f; 4. float[][] f2 = {{42.Of}, {l.7f, 2.3f}, {2.6f, 2.7f}}; 5. float[] f3 = {2.7f}; 6. Long x = 42L; 7. // insert code here 8. System.out.println("true"); 9. } 10. } And the following five code fragments: F1. if (f1 == f2) F2. if (f1 == f2[2][1]) F3. if (x == f2[0][0]) F4. if (f1 == f2 [1,1]) F5. if (f3 == f2 [2]) What is true?

  1. One of them will compile, only one will be true.

  2. Two of them will compile, only one will be true.

  3. Two of them will compile, two will be true.

  4. Three of them will compile, only one will be true

  5. Three of them will compile, exactly two will be true.

  6. Three of them will compile, exactly three will be true.

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

Analyzing each fragment: F1 (f1 == f2) won't compile - comparing primitive to 2D array. F2 (f1 == f2[2][1]) compiles and compares 2.3f == 2.7f, which is false. F3 (x == f2[0][0]) compiles with unboxing, 42L == 42.0f is true. F4 (f1 == f2[1,1]) won't compile - invalid comma syntax. F5 (f3 == f2[2]) compiles but compares array references, which is false. Thus F2, F3, F5 compile (3 total), but only F3 is true (1 true).

AI explanation

F1 fails to compile because you can't compare a float to a float[][] reference. F4 uses invalid array-access syntax (f2[1,1] instead of f2[1][1]), so it also fails to compile. That leaves F2, F3, and F5 as the ones that compile. F3 is true because x (a Long) unboxes and widens to 42.0f, matching f2[0][0]; F2 (2.3f == 2.7f) and F5 (comparing two distinct array references) are both false. So three fragments compile and exactly one (F3) evaluates true.