This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • 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 ...
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Doubt with Generic Collections

Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to everyone!

Currently, im preparing myself for the OCP SE 7 certification exam. I bought the book OCA/OCP Java SE 7 Programmer I & II Study Guide by Kathy Sierra and Bert Bates and dont understand one of the questions of the self Test. This one in particular is the number 13 in the Collections Topic of the interactive Quiz.

Which declarations could be inserted at // INSERT DECLARATION HERE so that the program will compile and run?

  • A: static public <E extends CharSequence> Collection<? extends CharSequence> getLongWords(Collection<E> coll)
  • B: static public <E extends CharSequence> List<E> getLongWords(Collection<E> coll)
  • C: static public <E extends CharSequence> getLongWords(Collection<E> coll)
  • D: static public List<CharSequence> getLongWords(Collection<CharSequence> coll)
  • E: static public List<? extends CharSequence> getLongWords(Collection<? extends CharSequence> coll) >Correct answer<
  • F: static public <E extends CharSequence> Collection<E> getLongWords(Collection<E> coll)
  • G: static public <E super CharSequence> Collection<E> getLongWords(Collection<E> coll)

  • First of all the CharSequence class confuses me.

    As i understand, the method returns a Collection<E> variable, so options C, D and E as they dont declare the return type, they cant be. As well as B, which returns a List, so it cant be neither. These ones are clear.

    So here is what i understand in option A:
    The method returns a Collection of type CharSequence or any of their subclasses. But the main method expects a Collection<String> so it wont compile.
    The option E is almost the same as A, but it just returns Collections only of type CharSequence. So it shouldnt compile. Of course it does because this is the correct answer.
    Option G is the same as the others but it could be CharSequence or any of his parent classes.

    The thing is, why is E correct because none of A, E or G matches the return type of Collection<String>

    Thanks in advance!

    Posts: 9643
    Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    so options C, D and E as they dont declare the return type

    Hi Javier, options D and E do specify a return type, only option C doesn't specify a return type.

    I agree with you, E is not the correct answer, it should be F. I believe this is an error in the book and there should be errata for this. You can always try such code out and see which code compiles...
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!