What is the result?

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

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

A.
Result: 10
Result: 30

B.
Result: 10
Result: 25

C.
Result: 235
Result: 215

D.
Result: 215
Result: 215

E.
Compilation fails

Explanation:
First line:
System.out.println(“Result: ” + 2 + 3 + 5);
String concatenation is produced.
Second line:
System.out.println(“Result: ” + 2 + 3 * 5);
3*5 is calculated to 15 and is appended to string 2. Result 215.
The output is:
Result: 235
Result: 215
Note #1:
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));
run:

Result: 10
Result: 7
Note #2:
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.



Leave a Reply 0

Your email address will not be published. Required fields are marked *