• 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

Generic Type java  RSS feed

 
Ranch Hand
Posts: 62
  • 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: 10230
216
  • 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.
 
Yup, yup, yup. Tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!