nerdexam
Oracle

1Z0-819 · Question #23

Given this code fragment: ``java String s = ""; if (Double.parseDouble("11.00F") > 11) { s += 1; } if (1 == Integer.valueOf("17")) { s += 2; } if (1024 > 1023L) { s += 3; } System.out.print(s); ``…

The correct answer is A. 23. Option A appears to be incorrect based on actual Java behavior - working through each condition reveals the true output is "3" (option D), which means the answer key for this question contains an error. Condition 1 - Double.parseDouble("11.00F") > 11: Java's parseDouble accepts…

Working with Java Data Types

Question

Given this code fragment:
String s = "";
if (Double.parseDouble("11.00F") > 11) {
 s += 1;
}
if (1 == Integer.valueOf("17")) {
 s += 2;
}
if (1024 > 1023L) {
 s += 3;
}
System.out.print(s);
What is the result?

Options

  • A23
  • B12
  • C13
  • D3

How the community answered

(36 responses)
  • A
    75% (27)
  • B
    3% (1)
  • C
    6% (2)
  • D
    17% (6)

Explanation

Option A appears to be incorrect based on actual Java behavior - working through each condition reveals the true output is "3" (option D), which means the answer key for this question contains an error.

Condition 1 - Double.parseDouble("11.00F") > 11: Java's parseDouble accepts the float type suffix (f, F, d, D), so "11.00F" parses successfully to 11.0. But 11.0 > 11 compares equal values (the int 11 widens to 11.0), so this is false - the first block is skipped.

Condition 2 - 1 == Integer.valueOf("17"): Integer.valueOf("17") returns Integer(17). Java unboxes it for the == comparison, giving 1 == 17, which is false - the second block is skipped.

Condition 3 - 1024 > 1023L: The int 1024 is widened to long 1024L, and 1024L > 1023L is true - s += 3 executes.

Only the third block runs, so s = "3" and the output is 3. Option D is correct; option A would require conditions 2 and 3 to be true simultaneously, but 1 == 17 is never true in Java. The memory tip worth keeping: when a == comparison mixes int and Integer, the Integer is always unboxed - object identity never enters the picture.

Topics

#type conversion#string concatenation#parsing#numeric comparison

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice