Java OCA OCP Practice Question 3163

Question

What will the program print when it is compiled and run?

import static java.lang.System.out;
import java.util.Collections;
import java.util.NavigableSet;
import java.util.TreeSet;

public class Main {
 public static void main(String[] args) {
   NavigableSet<String> strSetA = new TreeSet<String>();
   Collections.addAll(strSetA, "set", "shell", "soap");
   out.print(strSetA.ceiling("shell") + " ");
   out.print(strSetA.floor("shell") + " ");
   out.print(strSetA.higher("shell") + " ");
   out.println(strSetA.lower("shell"));
 }
}

Select the one correct answer.

  • (a) shell soap shell set
  • (b) soap set shell shell
  • (c) shell shell soap set
  • (d) set shell shell soap


(c), (d), (e), and (f)

Note

Sets cannot have duplicates.

HashSet does not guarantee the order of the elements in (a) and (b), therefore there is no guarantee that the program will print [1, 9].

Because LinkedHashSet maintains elements in insertion order in (c) and (d), the program is guaranteed to print [1, 9].

Because TreeSet maintains elements sorted according to the natural ordering in (e) and (f), the program is guaranteed to print [1, 9].




PreviousNext

Related