Java OCA OCP Practice Question 2927

Question

Given:

import java.util.Collections;
import java.util.TreeSet;

public class Main {
   public static void main(String[] args) {
      TreeSet<String> s = new TreeSet<String>();
      s.add("a");
      s.add("f");
      s.add("b");
      System.out.print(s + " ");
      Collections.reverse(s);//  w  w  w .ja va2  s.  co  m
      System.out.println(s);
   }
}

What is the result?

  • A. Compilation fails.
  • B. [a, b, f] [a, b, f]
  • C. [a, b, f] [f, b, a]
  • D. [a, f, b] [b, f, a]
  • E. [a, b, f], followed by an exception.
  • F. [a, f, b], followed by an exception.


A is correct.

Note

The Collections methods sort(), reverse(), and binarySearch() do not work on Sets.




PreviousNext

Related