Java OCA OCP Practice Question 2028

Question

What will the following program print when run?.

public class Main {
  public static void main (String[] args) {
    String cs1 = "JAVA";
    StringBuilder cs2 = new StringBuilder(cs1);
    System.out.println(cs1.compareTo(cs2) == cs2.compareTo(cs1));
  }
}

Select the one correct answer.

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


(c)

Note

The classes String and StringBuilder are unrelated, therefore the first call to the compareTo() method will not compile.

The class StringBuilder does not implement the Comparable interface, therefore the second call to the compareTo() method will not compile.




PreviousNext

Related