nerdexam
Oracle

1Z0-851 · Question #99

Given: 1. import java.util.*; 2. 3. public class Explorer3 { 4. public static void main(String[] args) { 5. TreeSet<Integer> s = new TreeSet<Integer>(); 6. TreeSet<Integer> subs = new…

The correct answer is B. An exception is thrown at runtime. Exception in thread "main" java.lang.IllegalArgumentException: key out of range at java.util.TreeMap$NavigableSubMap.put(TreeMap.java:1386) at java.util.TreeSet.add(TreeSet.java:238) at Explorer3.main(Explorer3.java:11)

Analyzing and Troubleshooting System Problems

Question

Given: 1. import java.util.*; 2. 3. public class Explorer3 { 4. public static void main(String[] args) { 5. TreeSet<Integer> s = new TreeSet<Integer>(); 6. TreeSet<Integer> subs = new TreeSet<Integer>(); 7. for (int i = 606; i < 613; i++) 8. if (i % 2 == 0) 9. s.add(i); 10. subs = (TreeSet) s.subSet(608, true, 611, true); 11. subs.add(629); 12. System.out.println(s + " " + subs); 13. } 14.} What is the result?

Options

  • ACompilation fails.
  • BAn exception is thrown at runtime.
  • C[608, 610, 612, 629] [608, 610]
  • D[608, 610, 612, 629] [608, 610, 629]
  • E[606, 608, 610, 612, 629] [608, 610]
  • F[606, 608, 610, 612, 629] [608, 610, 629]

How the community answered

(40 responses)
  • B
    73% (29)
  • C
    15% (6)
  • D
    3% (1)
  • E
    8% (3)
  • F
    3% (1)

Explanation

Exception in thread "main" java.lang.IllegalArgumentException: key out of range at java.util.TreeMap$NavigableSubMap.put(TreeMap.java:1386) at java.util.TreeSet.add(TreeSet.java:238) at Explorer3.main(Explorer3.java:11)

Topics

#Java TreeSet#subSet#IllegalArgumentException#NavigableSet bounds

Community Discussion

No community discussion yet for this question.

Full 1Z0-851 Practice