Java OCA OCP Practice Question 1887

Question

What will the following class print when run?

public class Main{
   public static void main (String [] args)   {
     String s1 = new String ("java");
     StringBuilder s2 = new StringBuilder ("java");
     replaceString (s1);/*  ww  w.  ja v a  2s . co  m*/
     replaceStringBuilder (s2);
     System.out.println (s1 + s2);
   }
  static void replaceString (String s)  {
     s = s.replace ('j',  'l');
   }
  static void replaceStringBuilder (StringBuilder s)  {
     s.append ("c");
   }
}

Select 1 option

A. javajava
B. lavajava
C. javajavac
D. lavajavac
E. None of these.


Correct Option is  : C

Note

String is immutable while StringBuilder is not.

So no matter what you do in replaceString() method, the original String that was passed to it will not change.

On the other hand, StringBuilder methods, such as replace or append, change the StringBuilder itself.

So, 'c' is appended to java in replaceStringBuilder() method.




PreviousNext

Related