Java OCA OCP Practice Question 676

Question

What is the output of the following?

StringBuilder teams = new StringBuilder("001"); 
teams.append(" 002"); 
teams.append(" 1234"); 
System.out.print(teams); 
  • A. 001
  • B. 001 002 1234
  • C. The code compiles but outputs something else.
  • D. The code does not compile.


B.

Note

Since StringBuilder is mutable, each call to append adds to the value.

When calling print, toString() is automatically called and 001 002 1234 is output.

Option B is correct.




PreviousNext

Related