Java OCA OCP Practice Question 2541

Question

Consider the following program and predict the output:

public class Main {
     public static void main(String []args) {
             String s = new String("5");
             System.out.println(1+10+s+1+10);
     }
}
  • a) 11511
  • b) 1105110
  • c) 115110
  • d) 27


c)

Note

The string concatenation operator works as follows:

if both the operands are numbers, it performs the addition;

otherwise it concatenates the arguments by calling the toString() method if needed.

It evaluates from left to right.

Hence, the expression in the program results in the string 115110.




PreviousNext

Related