Java OCA OCP Practice Question 2016

Question

What will the following program print when run?.

public class Main {
  private static void putO(String s1) {
    s1 = s1.trim();/*from  w w w.  j a va  2  s .c o  m*/
    s1 += "O";
  }

  public static void main(String[] args) {
    String s1 = " W ";
    putO(s1);
    s1.concat("W");
    System.out.println("|" + s1 + "|");
  }
}

Select the one correct answer.

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


(d)

Note

The call to the putO() method does not change the String object referred to by the s1 reference in the main() method.

The reference value returned by the call to the concat() method is ignored.




PreviousNext

Related