nerdexam
Oracle

1Z0-803 · Question #4

Given the code fragment: System.out.println("Result: " + 2 + 3 + 5); System.out.println("Result: " + 2 + 3 * 5); What is the result?

The correct answer is C. Result: 235. System.out.println("Result: " + 2 + 3 + 5); String concatenation is produced. System.out.println("Result: " + 2 + 3 5); 35 is calculated to 15 and is appended to string 2. Result 215. To produce an arithmetic result, the following code would have to be used…

Using Operators and Decision Constructs

Question

Given the code fragment:

System.out.println("Result: " + 2 + 3 + 5); System.out.println("Result: " + 2 + 3 * 5); What is the result?

Options

  • AResult: 10
  • BResult: 10
  • CResult: 235
  • DResult: 215
  • ECompilation fails

How the community answered

(49 responses)
  • A
    6% (3)
  • B
    2% (1)
  • C
    90% (44)
  • D
    2% (1)

Explanation

System.out.println("Result: " + 2 + 3 + 5); String concatenation is produced. System.out.println("Result: " + 2 + 3 * 5); 3*5 is calculated to 15 and is appended to string 2. Result 215. To produce an arithmetic result, the following code would have to be used: System.out.println("Result: " + (2 + 3 + 5)); System.out.println("Result: " + (2 + 1 * 5)); If the code was as follows: System.out.println("Result: " + 2 + 3 + 5"); System.out.println("Result: " + 2 + 1 * 5"); The compilation would fail. There is an unclosed string literal, 5", on each line.

Topics

#string concatenation#operator precedence#arithmetic operators

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice