File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes answer to this question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "answer to this question" Watch "answer to this question" New topic
Author

answer to this question

Mike Rudiger
Greenhorn

Joined: Jan 06, 2008
Posts: 2
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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

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


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
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?


Thanks and Regards
Mike Rudiger
Greenhorn

Joined: Jan 06, 2008
Posts: 2
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

Joined: Jan 01, 2007
Posts: 368
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
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
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)





Blog
Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
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.


If you worry you cannot work... If you work you need not worry
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: answer to this question