What is the result?

Given:

12. import java.util.*;
13. public class Explorer3 {
14. public static void main(String[] args) {
15. TreeSet<Integer> s = new TreeSet<Integer>();
16. TreeSet<Integer> subs = new TreeSet<Integer>();
17. for(int i = 606; i < 613; i++)
18. if(i%2 == 0) s.add(i);
19. subs = (TreeSet)s.subSet(608, true, 611, true);
20. subs.add(629);
21. System.out.println(s + ” ” + subs);
22. }
23. }

What is the result?

Given:

12. import java.util.*;
13. public class Explorer3 {
14. public static void main(String[] args) {
15. TreeSet<Integer> s = new TreeSet<Integer>();
16. TreeSet<Integer> subs = new TreeSet<Integer>();
17. for(int i = 606; i < 613; i++)
18. if(i%2 == 0) s.add(i);
19. subs = (TreeSet)s.subSet(608, true, 611, true);
20. subs.add(629);
21. System.out.println(s + ” ” + subs);
22. }
23. }

What is the result?

A.
Compilation fails.

B.
An 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]



Leave a Reply 2

Your email address will not be published. Required fields are marked *


Regina

Regina

The answer should be B, because 629 cannot be added to subs.

Exception in thread “main” java.lang.IllegalArgumentException: key out of range
at java.util.TreeMap$NavigableSubMap.put(Unknown Source)
at java.util.TreeSet.add(Unknown Source)
at aiotestking.Explorer3.main(Explorer3.java:12)

matti

matti

B is correct, not F