Java OCA OCP Practice Question 2687

Question

What is the result of the following code?

TreeSet<String> tree = new TreeSet<String>(); 
tree.add("one"); 
tree.add("One"); 
tree.add("ONE"); 
System.out.println(tree.ceiling("On")); 
  • A. On
  • B. one
  • C. One
  • D. ONE
  • E. The code does not compile.
  • F. An exception is thrown.


C.

Note

TreeSet sorts the elements.

Since uppercase letters sort before lowercase letters, the ordering is "ONE", "One", "one".

The ceiling() method returns the smallest element greater than the specified one.

"On" appears between "ONE" and "One".

Therefore, the smallest element that is larger than the specified value is "One".




PreviousNext

Related