Java OCA OCP Practice Question 3181

Question

Which statement is true about the following program?

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    List<StringBuilder> list = new ArrayList<StringBuilder>();
    list.add(new StringBuilder("B"));
    list.add(new StringBuilder("A"));
    list.add(new StringBuilder("C"));
    Collections.sort(list, Collections.reverseOrder());
    System.out.println(list.subList(1,2));
  }//from  ww  w .  jav a  2 s .c  om
}

Select the one correct answer.

  • (a) The program will compile and print the following when run: [B].
  • (b) The program will compile and print the following when run: [B, A].
  • (c) The program will compile, but throw an exception when run.
  • (d) The program will not compile.


(c)

Note

The class StringBuilder does not implement the Comparable interface.

The sort() method that takes a comparator does not place any such requirements on the element type.

The program compiles, but throws a ClassCastException, as StringBuilder objects cannot be compared in reverse natural ordering.




PreviousNext

Related