File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes scjp 1.5 question related to collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "scjp 1.5 question related to collections" Watch "scjp 1.5 question related to collections" New topic

scjp 1.5 question related to collections

vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
please help me with the explanation of this code and its answer!

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);
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)

Answer for this is option F

Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032

First of all, there is a rule that requires that you quote your sources.
Second, this question has been asked before. It might be good if you do a search before asking a question, because many times you will find some good answers already for it: Ultimate Generics Question



All code in my posts, unless a source is explicitly mentioned, is my own.
I agree. Here's the link:
subject: scjp 1.5 question related to collections
It's not a secret anymore!