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.
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?
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>
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...