Java OCA OCP Practice Question 3183

Question

Which statement is true about the following program?

// NEW RQ//from  ww  w .  jav a  2s  . c  o  m
import java.util.ArrayList;
import java.util.Arrays;
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("D"));
    list.add(new StringBuilder("C"));
    StringBuilder[] sbArray = list.toArray(new StringBuilder[0]);

    Collections.sort(list);
    Collections.sort(list, null);
    Collections.sort(list, Collections.reverseOrder());
    System.out.println("List: " + list);

    Arrays.sort(sbArray);
    Arrays.sort(sbArray, null);
    Arrays.sort(sbArray, Collections.reverseOrder());
    System.out.println("Array: " + Arrays.toString(sbArray));
  }
}

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.


(d)

Note

The class StringBuilder does not implement the Comparable interface.

The sort() method (without the comparator) requires that the element type of the list is Comparable.

The program will not compile.




PreviousNext

Related