• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Generic

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Below is the question:

import java.util.*;
public class BackLister{
//INSERT CODE HERE
{
List<T> output = new LinkedList<T>();
for(T t: input)
output.add(0, t);
return output;
}
}

Which of the following can be inserted at //INSERT CODE 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)






I replaced T with Animal and Cat extends Animal.Below is the program:






My question is "How is Answer D correct".As you can see if I replace T with Animal,I get compile -time error as expected.Please help me to explain.

 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kushtawar please QuoteYourSources so that we can help you with your problem...
 
C Kushtawar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is K.Sierra's MasterExam and the question number is 57.
 
Ankit Garg
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Khushwant you are getting the concept wrong here. When we write the return type as List<? extends T> then it means that the list will be of type T or a sub type of T. But in the code of the method, a list of type T is created and that is returned. So basically we are returning a List<T> from the method. You are trying to squeeze a List<Animal> to a List<Cat> which is not allowed...
 
C Kushtawar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit...

I have got it.

I should have replaced T with Animal in my example .

What I was doing was replacing ? extends T with Cat which rather at runtime will be ? extends Animal.

I think I have git the concept now.

Thanks.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



public static<T> List<T> backwards(List<? extends T> input)



and why B also correct answer?? within this code is trying to add something in output list but the method argument takes List<? extends T>. That forbid us to add something in list. what is exactly I'm missing in here?

thanks for any kind explanation
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

forbid us to add something in list





here we are adding t to the output LinkedList , not to the Input . If code is changed to



Then we will get the error as

BackLister.java:8: cannot find symbol
symbol : method add(int,T)
location: interface java.util.List<capture#967 of ? extends T>
input.add(0, t);
^
1 error

Regards
Sunny Mattas
SCJP5
 
Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic