Java OCA OCP Practice Question 2032

Question

What will the following program print when run?.

public class Main {
 private static StringBuilder putO(StringBuilder s1) {
   s1.append("O");
   return s1;//from  w  w w.  j  ava  2  s  .com
 }

 public static void main(String[] args) {
   StringBuilder s1 = new StringBuilder("W");
   boolean status = putO(s1).append("W!").toString().compareTo("WOW!") != 0;
   System.out.println(s1 + " " + status);
 }
}

Select the one correct answer.

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


(a)

Note

The type of the return value from the chain of calls is StringBuilder, StringBuilder, String, and int, respectively.

The string builder contents are changed to "WOW!" by the two first calls in the chain.

The toString() call extracts the character sequence, which is compared with the string literal "WOW".

The compareTo() method returns the value 0.

The boolean expression evaluates to false.




PreviousNext

Related