Java OCA OCP Practice Question 3020

Question

Given:

import java.util.TreeSet;

public class Main {
   public static void main(String[] args) {
      TreeSet<String> t1 = new TreeSet<String>();
      TreeSet<String> t2 = new TreeSet<String>();
      t1.add("b");
      t1.add("7");
      t2 = (TreeSet) t1.subSet("5", "c");
      try {/*from  w  ww .  j  a v  a  2 s . c  o  m*/
         t1.add("d");
         t2.add("6");
         t2.add("3");
      } catch (Exception e) {
         System.out.print("ex ");
      }
      System.out.println(t1 + " " + t2);
   }
}

What is the result?

  • A. [6, 7, b] [6, 7, b]
  • B. [6, 7, b, d] [6, 7, b]
  • C. ex [6, 7, b] [6, 7, b]
  • D. ex [6, 7, b, d] [6, 7, b]
  • E. [3, 6, 7, b, d] [6, 7, b]
  • F. ex [6, 7, b, d] [6, 7, b, d]
  • G. Compilation fails due to error(s) in the code.


D is correct.

Note

The subSet() method is creating a "backed" collection with a certain value range.

Additions to either collection that are in the backed collection's range will be added to both.

Attempting to add an out-of-range entry to the backed collection throws an exception.




PreviousNext

Related