Java OCA OCP Practice Question 2943

Question

Given:

import java.util.Iterator;
import java.util.TreeMap;
import java.util.TreeSet;

public class Main {
   public static void main(String[] args) {
      TreeMap<String, String> tm = new TreeMap<String, String>();
      TreeSet<String> ts = new TreeSet<String>();
      String[] k = { "1", "b", "4", "3" };
      String[] v = { "a", "d", "3", "b" };
      for (int i = 0; i < 4; i++) {
         tm.put(k[i], v[i]);/*from  ww w .j av  a  2s .  co  m*/
         ts.add(v[i]);
      }
      System.out.print(tm.values() + " ");
      Iterator it2 = ts.iterator();
      while (it2.hasNext())
         System.out.print(it2.next() + "-");
   }
}

Which of the following could be a part of the output? (Choose two.)

  • A. [a, b, 3, d]
  • B. [d, a, b, 3]
  • C. [3, a, b, d]
  • D. [a, b, d, 3]
  • E. [1, 3, 4, b]
  • F. [b, 1, 3, 4]
  • G. 3-a-b-d-
  • H. a-b-d-3-
  • I. a-d-3-b-


A and G are correct.

Note

TreeMap.values() returns the values associated with sorted keys (in this case, "naturally sorted" keys).

TreeSet iterators use the sorting sequence defined by the instance of TreeSet being used.

In this case, the TreeSet was built using "natural ordering.".




PreviousNext

Related