• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help me understand

 
Sree Potluri
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

public class Test {
//insert here
{
List<T> output = new LinkedList<T>();
for (T t:input)
output.add(0,t);
return output;
}
}

which can be inserted //insert here to compile and run without error
A. public static <T> List <T> backwards(List<T> input)
B. public static <T> List <T> backwards(List<? extends T> input)
C. public static <T> List <T> backwards(List<? super T> input)
D. public static <T> List <? extends T> backwards(List<T> input)
E. public static <T> List <? super T> backwards(List<T> input)
F. public static <? extends T> List <T> backwards(List<T> input)
G. public static <? super T> List <T> backwards(List<T> input)

Correct answer A, B, D, E

Help me understand this
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey what didnt you understand? you must have eliminated something by yourself....just asking....
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why do you think C is wrong whereas B is right?
 
Sree Potluri
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I determine where to use "extends" and where to use "super" in this kind of scenario. Do these type of questions appear in exam?
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup i faced a similar kind of question in the exam
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suppose in either B or C you pass in a List<Animal>.....so that means T is animal...in the ? extends Animal option what you get outside is an animal, that is, the return type is animal...so in the B option T is the return type..which is right...but in ? super Animal you can pass in anything that is the superclass of super...which means you only get an object out of it...so the return type cant be T or animal..this is why C is wrong..understood anything?
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.coderanch.com/t/474163/Programmer-Certification-SCJP/certification/generics-extends
 
Sree Potluri
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Raju, but can you give me another example.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think there are enough examples in the above link
 
Sree Potluri
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help Raju
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4598
303
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at this topic too.
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sree please Quote Your Sources when you post a question...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic