• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Generic Type java  RSS feed

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
iI have the snippet of following code

Which method header can be inserted at (1) so that the program compiles and runs
without errors?
Select the one correct answer.
(a) static <E extends CharSequence> Collection<? extends CharSequence>
delete4LetterWords(Collection<E> words)
(b) static <E extends CharSequence> List<E>
delete4LetterWords(Collection<E> words)
(c) static <E extends CharSequence> Collection<E>
delete4LetterWords(Collection<? extends CharSequence> words)
(d) static <E extends CharSequence> List<E>
delete4LetterWords(Collection<? extends CharSequence> words)
(e) static <E extends CharSequence> Collection<E>
delete4LetterWords(Collection<E> words)
(f) public <E super CharSequence> Collection<E>
delete4LetterWords(Collection<E> words)
the correct answer is (e)
please someone how telle me why the ather suggestion are not true i found a  confusion betwenn them to specify the correct answer
 
Saloon Keeper
Posts: 10136
214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not allowed because resultList on line 4 expects a return type of Collection<String>, and this method returns a Collection<? extends CharSequence>.

This is not allowed because permittedWords on line 8 is a Collection<String>, which you can't return if the return type is a List<String>.

This is not allowed because the loop variable word on line 9 expects String, but the words parameter is a Collection<? extends CharSequence>, meaning the elements are not necessarily String.

This is not allowed for the same reason as option b): You can't return a Collection if the return type is a List.

This is allowed.

This is not allowed because the length() method on line 10 is defined for CharSequence, and E is not necessarily CharSequence, it could be Object.

By the way, I assumed that the public keyword in the last option was supposed to be the static keyword.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!