Java OCA OCP Practice Question 702

Question

What is the output of the following?

String teams = new String("001"); 
teams.concat(" 1234"); 
teams.concat(" 5678"); 
teams.concat(" 9012"); 
System.out.println(teams); /*from   ww  w .jav  a 2 s  .  c  o  m*/

     A.    001 

     B.    001 1234 5678 9012 

     C.   The code compiles but outputs something else. 

     D.   The code does not compile. 


A.

Note

Since String is immutable, each call to concat() returns a new object with the new value.

However, that return value is ignored and the teams variable never changes in value.

Therefore it stays as 001, and Option A is correct.




PreviousNext

Related