Wrong answer in Pass4Sure 1Z0-851

Hi every one…warning-sign

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 to toElement. If fromElement and toElement are equal, the returned set is empty unless fromExclusive and toExclusive 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 interface NavigableSet<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, to toElement, exclusive
Throws :
ClassCastException - if fromElement and toElement 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 if fromElement or toElement cannot be compared to elements currently in the set.
NullPointerException - if fromElement or toElement is null and this set uses natural ordering, or its comparator does not permit null elements
IllegalArgumentException - if fromElement is greater than toElement; or if this set itself has a restricted range, and fromElement ortoElement lies outside the bounds of the range.
Since :
1.6

ليست هناك تعليقات:

إرسال تعليق

---- أتشرف بتعليقاتكم ----