Java OCA OCP Practice Question 455

Question

What is the output of the following application?


public class MyClass { 
        public static final void main(String[] days) { 
           System.out.print(5 + 6 + "7" + 8 + 9); 
        } 
} 
  • A. 56789
  • B. 11789
  • C. 11717
  • D. The code does not compile.


B.

Note

Remember that Java evaluates + from left to right.

The first two values are both numbers, so the + is evaluated as numeric addition, resulting in a reduction to 11 + "7" + 8 + 9.

The next two terms, 11 + "7", are handled as string concatenation since one of the terms is a String.

This allows us to reduce the expression to "117" + 8 + 9.

The final two terms are each evaluated one at a time with the String on the left.

Therefore, the final value is 11789, making Option B the correct answer.




PreviousNext

Related