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?
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]
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(Main.java:11)
B
This question is similar to Q 167. adding 609 will show up in both sets but adding 629 should add only on set “S”. So the answer is E here.
Basically both questions do *NOT* compile at all unless the following change is made
subs = (TreeSet)s.subSet(608, true, 611, true);
So answer is A, strictly speaking for both these questions.
Sorry the change should be as follows
subs = (TreeSet)s.subSet(608, true, 611, true);
For some reasons the “” are not coming up. I guess it is HTML stuff.
subs = (TreeSet )s.subSet(608, true, 611, true);
The line should be modified as below. Otherwise the code will not even compile.
subs = (TreeSet <Integer>)s.subSet(608, true, 611, true);
subs = (TreeSet <Integer>)s.subSet(608, true, 611, true);
you can test the code here online
https://www.compilejava.net/
Interestingly when I run the code on Netbeans 8.2, Answer is “B” as follows
run:
Exception in thread “main” java.lang.IllegalArgumentException: key out of range
at java.util.TreeMap$NavigableSubMap.put(TreeMap.java:1512)
at java.util.TreeSet.add(TreeSet.java:255)
at introprogramming.Main.main(Main.java:36)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Finally I found the details. There are two or three questions of this type and everything depends on the following line.
11. subs.add(629); // adding to subset
One question has
11. s.add(629); // adding to original set
And another one has
11. subs.add(609); // adding a number inside the range
Adding 609 should not be a problem to either set (SUBS or S) but adding 629 to S will work and fail for SUBS (because for subs the min is 608 and max is 611)
Lesson learned – Read the question slowly and look what it is asking.
Now you run into trouble because you try to add “629” to t2, which is out of range [ 608 – 611]de subSet(608, true,611,true);, thus causing the IllegalArgumentException which can be seen in the log output. The element is not inserted into subs
so the correct answer is B
Now you run into trouble because you try to add “629” to subs, which is out of range [ 608 – 611]de subSet(608, true,611,true);, thus causing the IllegalArgumentException which can be seen in the log output. The element is not inserted into subs
so the correct answer is B