There is a question that have been solved wrongly in Pass4Sure in SCJP 1Z0-851 [1/6/2011].
Here is the question:
4.Given:
12. import java.util.*;
13. public class Explorer2 {
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]
The answer of Pass4Sure was : F
But the answer must be : B
Because you add an element out of the range the subs was made for.
I have tried it on NetBeans, the class:
import java.util.TreeSet;
/**
*
* @author Ahmad Attallah -www.hope-sun.net-
*/
public class TestClass {
public static void main(String[] args) {
TreeSet<Integer> s = new TreeSet<Integer>();
TreeSet<Integer> subs = new TreeSet<Integer>();
for (int i = 606; i < 613; i++)
if (i % 2 == 0)
s.add(i);
subs = (TreeSet<Integer>) s.subSet(608, true, 611, true);
subs.add(629);
System.out.println(s + " " + subs);
}
}
The exception was :
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 addressbookapp.TestClass.main(TestClass.java:45)
Java Result: 1
You could see the illustration of subSet method:
subSet
public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive)
-
Description copied from interface:
NavigableSet
-
Returns a view of the portion of this set whose elements range from
fromElement
totoElement
. IffromElement
andtoElement
are equal, the returned set is empty unlessfromExclusive
andtoExclusive
are both true. The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The returned set supports all optional set operations that this set supports.The returned set will throw an
IllegalArgumentException
on an attempt to insert an element outside its range.
-
Specified by :
-
subSet
in interfaceNavigableSet<E>
-
-
Parameters :
-
fromElement
- low endpoint of the returned set -
fromInclusive
-true
if the low endpoint is to be included in the returned view -
toElement
- high endpoint of the returned set -
toInclusive
-true
if the high endpoint is to be included in the returned view -
Returns :
-
a view of the portion of this set whose elements range from
fromElement
, inclusive, totoElement
, exclusive -
Throws :
-
ClassCastException
- iffromElement
andtoElement
cannot be compared to one another using this set's comparator (or, if the set has no comparator, using natural ordering). Implementations may, but are not required to, throw this exception iffromElement
ortoElement
cannot be compared to elements currently in the set. -
NullPointerException
- iffromElement
ortoElement
is null and this set uses natural ordering, or its comparator does not permit null elements -
IllegalArgumentException
- iffromElement
is greater thantoElement
; or if this set itself has a restricted range, andfromElement
ortoElement
lies outside the bounds of the range. -
Since :
-
1.6
-
ليست هناك تعليقات:
إرسال تعليق
---- أتشرف بتعليقاتكم ----