• 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
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

answer to this question  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone tell me what this answer is? I think its C but thats not what the answer is.

Given a method declared as:
public static <E extends Number> List<? super E> process(List<E> nums)
A programmer wants to use this method like this:
// INSERT DECLARATIONS HERE
output = process(input);
Which pairs of declarations could be placed at // INSERT DECLARATIONS HERE to allow
the code to compile? (Choose all that apply.)
A. ArrayList<Integer> input = null;
ArrayList<Integer> output = null;
B. ArrayList<Integer> input = null;
List<Integer> output = null;
C. ArrayList<Integer> input = null;
List<Number> output = null;
D. List<Number> input = null;
ArrayList<Integer> output = null;
E. List<Number> input = null;
List<Number> output = null;
F. List<Integer> input = null;
List<Integer> output = null;
G. None of the above.
 
Java Cowboy
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Joe Usenet", welcome to JavaRanch.

First, please check your private messages. You can see them by clicking My Private Messages.

If you copy a question from a book or mock exam then please quote your sources.
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike

Just curious to know what is the answer. Is it "G. None of the above."? Also what makes you think that C should be the answer? Also could you please quote your source?
 
Mike Rudiger
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the answer is b, e, and f and it also says this:

"The return type of process is definitely declared as a List, not an ArrayList, so A and D
are wrong. C is wrong because the return type evaluates to List<Integer>, and that can't
be assigned to a variable of type List<Number>. Of course all these would probably cause a
NullPointerException since the variables are still null´┐Żbut the question only asked us
to get the code to compile."

It is from the Cathy Sierra
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer for this should be "None of the above" unless something is seriously wrong with my logic. The compiler(1.5.11) agrees with the discussion below:



Method Argument is of type List<E> where <E extends Number>. Hence the method argument can be a List of objects which extend number. For e.g. List<Float>, List<Double>, List<Integer> are all valid.

Method Return Type is List<? super E> where <E extends Number>. Hence the method return type is a list of objects which are superclass of an object which itself extends number. Hence return type is undefined due to ambiguity(it may be Number or some superclass of Number or Object etc. etc.)

Now moving to the options:

A. ArrayList<Integer> input = null;
ArrayList<Integer> output = null;
This is obviously wrong as return type of process is List which can never be assigned to an ArrayList without casting.

B. ArrayList<Integer> input = null;
List<Integer> output = null;
This is wrong as the return type is ambiguous(See discussion above on Method Return Type). As it is ambiguous, it cannot be List<Integer>

C. ArrayList<Integer> input = null;
List<Number> output = null;
This is wrong as the return type is ambiguous(See discussion above on Method Return Type). As it is ambiguous, it cannot be List<Number>

D. List<Number> input = null;
ArrayList<Integer> output = null;
This is obviously wrong as return type of process is List which can never be assigned to an ArrayList without casting.

E. List<Number> input = null;
List<Number> output = null;
This is wrong as the return type is ambiguous(See discussion above on Method Return Type). As it is ambiguous, it cannot be List<Number>. Also the method argument is also wrong as it can be a List of objects which extend number but here it is List<Number>.

F. List<Integer> input = null;
List<Integer> output = null;
This is wrong as the return type is ambiguous(See discussion above on Method Return Type). As it is ambiguous, it cannot be List<Integer>

G. None of the above.
Correct!!

Please comment if this resoning is correct/incorrect.

Regards
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Satya,

Am pasting this from the K&B errata:

620....bug.......Q-16: method declaration s/b:
& 634
public static <E extends Number> List<E> process(List<E> nums)




 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still I dont understand the output. return type List<E> so we can return only a List and E could be an Integer. But how can answer B be right. the method argument is a List and in option B we are sending ArrayList. Can we send subtype of a Collection as an argument??

I have exam tomorrow morning and I am banging my head with Generics.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

the method argument is a List and in option B we are sending ArrayList. Can we send subtype of a Collection as an argument??



Yes of course, an ArrayList is always a List.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a question  regarding the explanation  of  first option(A) which says ,"The return type of process is definitely declared as a List, not an ArrayList" but we can return an ArrayList to a method which has its return type declared as List,right?
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kumar Biplav wrote:Hi,
I have a question  regarding the explanation  of  first option(A) which says ,"The return type of process is definitely declared as a List, not an ArrayList" but we can return an ArrayList to a method which has its return type declared as List,right?


You can return object of subclass from the method, but according to the method signature it will be "downgraded" to super class due to polymorphism nature. So, a returned ArrayList would be returned as List. To make it back to an ArrayList you'll need to perform an explicit cast. That's why an option A is not correct as well as other options having not List as an output.
 
Marshal
Posts: 63795
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your explanation is correct (), but not quite.

Tyoma Sakurakoji wrote:. . . according to the method signature

The return type isn't part of the method signature.

it will be "downgraded" to super class due to polymorphism nature.

Say supertype, not superclass; java.util.List isn't a class. That is an example of inheritance, not of polymorphism.

So, a returned ArrayList would be returned as List. To make it back to an ArrayList you'll need to perform an explicit cast. That's why an option A is not correct . . . .

That is the right explanation. In real life, you would declare your ArrayList as List:-\you can change line 6 to read words = new LinkedList<>(); and nothing strange will happen.
 
please buy this thing and then I get a fat cut of the action:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!