• 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

K&B Java5: Generics question #16 on page 620

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

On the Errata page, this question is flagged for an error, but the answer for the text is not updated.

Is the following a valid answer to #16 on page 620 of the K&B SCJP Study Guide for Java 5:

16. Given a method declared as:





A programmer wants to use this method like this

// INSERT DECLARATION HERE

output = process(input);



The following answers seems to work in this situation: A, B, E, F

A. ArrayList< Integer > input = null;
ArrayList< Integer > output = null;

B. ArrayList< Integer > input = null;
List<Integer> output = null;

E. List< Number > input = null;
List< Number > output = null;

F. List< Integer > input = null;
List < Integer > output = null;

ArrayList<Integer> IS-A List<Integer>, isn't it?
The following would not work, right?

C. ArrayList< Integer > input = null;
List< Number > output = null;

D. List< Number > input = null;
ArrayList< Integer > output = null;


THANKS. -Harry
 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The K&B SCJP Study Guide gives B, E, and F as answers.

The return type of process() is definitely declared as a List, not an ArrayList, so A and D are wrong.



Isn't ArrayList<Integer> a covariant return type for List<Integer> ?

Thanks. -Harry
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your assuming that the method will return an ArrayList and that may not be true.



This code won't compile. Therefore, A won't work all the time.
 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I didn't think of your usecase. - Harry
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic