Java OCA OCP Practice Question 886

Question

What is the result of the following code?

public class Main {
   public void s(String s1, StringBuilder s2) {
      s1.concat("!!!");
      s2.append("!!!");
   }/*from   w w  w .  j  a  v a 2s.  c o  m*/

   public static void main(String[] args) {
      String s1 = "s";
      StringBuilder s2 = new StringBuilder("s");
      new Main().s(s1, s2);
      System.out.println(s1 + " " + s2);
   }
}
  • A. s s
  • B. s s!!!
  • C. s!!! s
  • D. s!!! s!!!
  • E. An exception is thrown.
  • F. The code does not compile.


B.

Note

A String is immutable.

Calling concat() returns a new String but does not change the original.

A StringBuilder is mutable.

Calling append() adds characters to the existing character sequence along with returning a reference to the same object.




PreviousNext

Related