Java OCA OCP Practice Question 846

Question

What will the following code print when compiled and run?

public class Main  { 
  public static void main (String [] args)  { 

    String s = "A"; 
    StringBuilder sb = new StringBuilder (s); 
    s.append ("B"); 
    sb .append ("C"); 
     // w ww  . ja  va 2 s .com
    System .out.println (s); 
    System .out.println (sb); 
   } 
} 

Select 1 option

  • A. A and AC
  • B. AB and AC
  • C. A and ABC
  • D. It will not compile.


Correct Option is  : D

Note

append () method does not exist in String class.

It exits only in StringBuffer and StringBuilder. The value of sb will be AC though.




PreviousNext

Related