Java OCA OCP Practice Question 686

Question

What is the output of the following?

12:  StringBuilder b = new StringBuilder("12"); 
13:  b = b.append("3"); 
14:  b.reverse(); 
15:  System.out.println(b.toString()); 
  • A. 12
  • B. 123
  • C. 321
  • D. The code does not compile.


C.

Note

On line 12, the value of the StringBuilder is 12. On line 13, it becomes 123.

Since StringBuilder is mutable, storing the result in the same reference is redundant.

Then on line 14, the value is reversed, giving us 321 and making Option C correct.




PreviousNext

Related