nerdexam
Oracle

1Z0-808 · Question #56

Given: System.out.println("5 + 2 = " + 3 + 4); System.out.println("5 + 2 = " + (3 + 4)); What is the result?

The correct answer is D. 5 + 2 = 34 5 + 2 = 7. Option D is correct because Java evaluates + left-to-right: in line 1, "5 + 2 = " + 3 produces the string "5 + 2 = 3", and then concatenating 4 appends it as a character, yielding "5 + 2 = 34". In line 2, the parentheses force 3 + 4 to be evaluated as arithmetic first (result…

Using Operators and Decision Constructs

Question

Given: System.out.println("5 + 2 = " + 3 + 4); System.out.println("5 + 2 = " + (3 + 4)); What is the result?

Options

  • A5 + 2 = 34 5 + 2 = 34
  • B5 + 2 = 3 + 4 5 + 2 = 7
  • C7 = 7 7 + 7
  • D5 + 2 = 34 5 + 2 = 7

How the community answered

(31 responses)
  • B
    3% (1)
  • C
    3% (1)
  • D
    94% (29)

Explanation

Option D is correct because Java evaluates + left-to-right: in line 1, "5 + 2 = " + 3 produces the string "5 + 2 = 3", and then concatenating 4 appends it as a character, yielding "5 + 2 = 34". In line 2, the parentheses force 3 + 4 to be evaluated as arithmetic first (result: 7), which is then concatenated to the string, yielding "5 + 2 = 7".

Why the distractors fail:

  • A is wrong because the second line does produce 7, not 34 - parentheses matter.
  • B is wrong because "5 + 2 = " + 3 + 4 doesn't print the literal text 3 + 4; it concatenates the numbers individually as strings.
  • C is entirely fabricated output that matches neither expression.

Memory tip: Think of a String + chain as a "contamination" rule - once a String appears on the left side of +, every subsequent + to the right is concatenation, not math. Parentheses are your escape hatch to force arithmetic before the string takes over.

Topics

#string concatenation#operator precedence#type coercion#left-to-right evaluation

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice