aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B Mock question on generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B Mock question on generics" Watch "K&B Mock question on generics" New topic
Author

K&B Mock question on generics

Jari Timonen
Ranch Hand

Joined: Jan 16, 2004
Posts: 230
Hello,

Ref: K&B Chapter 7: Generics and Collections
Question/Answer 16 in page 634:


Given a method declared as:

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.


According to book the answer is B, E and F.

I answered G since they all look like incorrect to me (maybe some more studying with generics).

So i decided to test: Nothing worked.
See comments in the code.


public class SCJP {
// if you rename return type List<? super E> to List<E> it's compiling
public static <E extends Number> List<? super E> process(List<E> nums){
List <E> l = null; //is this correct?
return l;
}
public static void main(String[] args){
//answer B
ArrayList<Integer> input = null;
List<Integer> output = null;
output = process(input); // incompatible types says Intellij Idea 7.0.1 if return type is List<? super E>
}
}



Can some one please explain me what is correct and what's not. What i'm missing here?


SCJP 5.0, SCJD URLyBird 1.3.3, OCMJEA 5 (SCEA) Factory Homes
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Check the Errata, the correct method signature is:
public static <E extends Number> List<E> process(List<E> nums) {
Jari Timonen
Ranch Hand

Joined: Jan 16, 2004
Posts: 230
Method signature is copied straight from pdf.. That's why i'm asking
Althoug it's missing from the question in my first post.
[ November 24, 2007: Message edited by: Jari Timonen ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B Mock question on generics