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…
Question
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)- B3% (1)
- C3% (1)
- D94% (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, not34- parentheses matter. - B is wrong because
"5 + 2 = " + 3 + 4doesn't print the literal text3 + 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
Community Discussion
No community discussion yet for this question.