Java OCA OCP Practice Question 1216

Question

Given the proper import statement(s) and:

13.  TreeSet<String> s = new TreeSet<String>();
14.  TreeSet<String> subs = new TreeSet<String>();
15.  s.add("a"); s.add("b"); s.add("c"); s.add("d"); s.add("e");
16.//from   ww  w.  java 2 s .com
17.  subs = (TreeSet)s.subSet("b", true, "d", true);
18.  s.add("g");
19.  s.pollFirst();
20.  s.pollFirst();
21.  s.add("c2");
22.  System.out.println(s.size() +" "+ subs.size());

Which are true? (Choose all that apply.)

  • A. The size of s is 4
  • B. The size of s is 5
  • C. The size of s is 7
  • D. The size of subs is 1
  • E. The size of subs is 2
  • F. The size of subs is 3
  • G. The size of subs is 4
  • H. An exception is thrown at runtime


B and F are correct.

Note

After "g" is added, TreeSet s contains six elements and TreeSet subs contains three (b, c, d), because "g" is out of the range of subs.

The first pollFirst() finds and removes only the "a".

The second pollFirst() finds and removes the "b" from both TreeSet objects.

The final add() is in range of both TreeSets.

The final contents are [c,c2,d,e,g] and [c,c2,d].




PreviousNext

Related