Java OCA OCP Practice Question 973

Question

Which of the following are output by the following code? (Choose all that apply)

public class Main { 
 public static StringBuilder work(StringBuilder a,  StringBuilder b) { 
   a = new StringBuilder("a"); 
   b.append("b"); 
   return a; /*  w  w  w  .j a  v a  2 s . com*/
 } 
 public static void main(String[] args) { 
   StringBuilder s1 = new StringBuilder("s1"); 
   StringBuilder s2 = new StringBuilder("s2"); 
   StringBuilder s3 = work(s1, s2); 
   System.out.println("s1 = " + s1); 
   System.out.println("s2 = " + s2); 
   System.out.println("s3 = " + s3); 
 } 
} 
  • A. s1 = a
  • B. s1 = s1
  • C. s2 = s2
  • D. s2 = s2b
  • E. s3 = a
  • F. s3 = null
  • G. The code does not compile.


B, D, E.

Note

Since Java is pass-by-reference, assigning a new object to a does not change the caller.

Calling append() does affect the caller because both the method parameter and caller have a reference to the same object.

Finally, returning a value does pass the reference to the caller for assignment to s3.




PreviousNext

Related