aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes E super CharSequence makes no sense?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "E super CharSequence makes no sense??" Watch "E super CharSequence makes no sense??" New topic
Author

E super CharSequence makes no sense??

AbhishekSonu Gupta
Greenhorn

Joined: Jul 09, 2008
Posts: 2
Can anyone explain why Option G is wrong in question given below(Chapter-7 question 14 of SCJP 1.5 book by kathy sierra)..???

Q. Given that String implements java.lang.CharSequence, and:
import java.util.*;
public class LongWordFinder {
public static void main(String[] args) {
String[] array = { "123", "12345678", "1", "12", "1234567890"};
List<String> list = Arrays.asList(array);
Collection<String> resultList = getLongWords(list);
}
// INSERT DECLARATION HERE
{
Collection<E> longWords = new ArrayList<E>();
for (E word : coll)
if (word.length() > 6) longWords.add(word);
return longWords;
} }

Which declarations could be inserted at // INSERT DECLARATION HERE so that the program
will compile and run? (Choose all that apply.)
A. public static <E extends CharSequence> Collection<? extends CharSequence>
getLongWords(Collection<E> coll)
B. public static <E extends CharSequence> List<E>
getLongWords(Collection<E> coll)
C. public static Collection<E extends CharSequence> getLongWords(Collection<E> coll)
D. public static List<CharSequence>
getLongWords(Collection<CharSequence> coll)
E. public static List<? extends CharSequence>
getLongWords(Collection<? extends CharSequence> coll)
F. static public <E extends CharSequence> Collection<E>
getLongWords(Collection<E> coll)
G. static public <E super CharSequence> Collection<E>
getLongWords(Collection<E> coll)

In Answer explaination it is given that Option G is wrong because
E super CharSequence makes no sense�super could be used in conjunction with a wildcard but not a type variable like E.
Keith Nagle
Ranch Hand

Joined: May 06, 2008
Posts: 65
CharSequence is an interface.
Look at F, this answer is the correct version of G.
Looking at the type parameter for G <E super CharSequence>
would imply that you are declaring E as a CharSequence or a
super entity of CharSequence, not something that implements the CharSequence interface. It is an interface, so you
have to have the keyword extends which means a type that "implements" or
"is-a" CharSequence. Read page 593 again.
Best regards


SCJP 5.0
AbhishekSonu Gupta
Greenhorn

Joined: Jul 09, 2008
Posts: 2
Thanks for your reply...

Tell me one more thing, if we take some other CLASS in place of CharSequence<interface> then would the option G be correct??
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: E super CharSequence makes no sense??