Java OCA OCP Practice Question 3249

Question

What will be written to the standard output when the following program is run?

public class Main {
  public static void main(String[] args) {
    String space = " ";

    String composite = space + "hello" + space + space;
    composite.concat("world");

    String trimmed = composite.trim();

    System.out.println(trimmed.length());
  }/*from ww  w .j ava2  s .  c o  m*/
}

Select the one correct answer.

  • (a) 5
  • (b) 6
  • (c) 7
  • (d) 12
  • (e) 13


(a)

Note

Strings are immutable, therefore, the concat() method has no effect on the original String object.

The string on which the trim() method is called consists of 8 characters, where the first and the two last characters are spaces (" hello ").

The trim() method returns a new String object where the white space characters at each end have been removed.

This leaves the 5 characters of the word "hello".




PreviousNext

Related