Java OCA OCP Practice Question 2020

Question

What will be the result of attempting to compile and run the following program?.

public class Main {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder("have a nice day");
    sb.setLength(6);
    System.out.println(sb);
  }
}

Select the one correct answer.

  • (a) The code will fail to compile because there is no method named setLength() in the StringBuilder class.
  • (b) The code will fail to compile because the StringBuilder reference sb is not a legal argument to the println() method.
  • (c) The program will throw a StringIndexOutOfBoundsException, when run.
  • (d) The program will print have a nice day, when run.
  • (e) The program will print have a, when run.
  • (f) The program will print ce day, when run.


(e)

Note

The program will compile without errors and will print have a when run.

The contents of the string buffer are truncated down to 6 characters.




PreviousNext

Related