Java OCA OCP Practice Question 295

Question

Given:

public class Main {
  public static void main(String[] args) {
    String s = "abc";
    System.out.println(">" + doStuff(s) + "<");
  }/*  ww w. jav a 2 s  .c o m*/
  static String doStuff(String s) {
    s = s.concat(" ef h ");
    return s.trim();
  }
}

What is the result?
      A.  >abcefh<
      B.  >efhabc<
      C.  >abc ef h<
      D.  >>ef h abc<
      E.  >abc ef h <



    C is correct.

    Note

    The concat() method adds to the end of the String, not the front.

    The trickier part is the return statement.

    Remember that Strings are immutable.

    The String referred to by "s" in doStuff() is not changed by the trim() method.

    Instead, a new String object is created via the trim() method, and a reference to that new String is returned to main().




    PreviousNext

    Related