• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

scjp 1.5 question related to collections  RSS feed

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!