• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

scjp 1.5 question related to collections

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
// 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)


Answer for this is option F

 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vijay,

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

Thanks,

Ruben
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic