Java OCA OCP Practice Question 2030

Question

What will the following program print when run?.

public class Main {
  private static void putO(StringBuilder s1) {
    s1 = s1.append("O");
  }/*w w w.j  a va2s . c o  m*/

  public static void main(String[] args) {
    StringBuilder s1 = new StringBuilder("W");
    putO(s1);
    s1.append("W!");
    System.out.println(s1);
  }
}

Select the one correct answer.

  • (a) The program will print WW!.
  • (b) The program will print WOW!.
  • (c) The program will print W.
  • (d) The program will fail to compile.
  • (e) The program will compile, but throw an exception at runtime.


(b)

Note

The call to the putO() method changes the StringBuilder object referred to by the s1 reference in the main() method.

So does the call to the append() method.




PreviousNext

Related