File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics

 
Sujatha Musunuri
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
11. public void addStrings(List list) {
12. list.add(�foo�);
13. list.add(�bar�);
14. }

What must you change in this method to compile without warnings?

A. add this code after line 11:
list = (List<String> list;
B. change lines 12 and 13 to:
list.add<String>(�foo�);
list.add<String>(�bar�);
C. change the method signature on line 11 to:
public void addStrings(List<? extends String> list) {
D. change the method signature on line 11 to:
public void addStrings(List<? super String> list) {
E. No changes are necessary. This method compiles without warnings


The Ans is D. But I think the answer is E. please correct me if I'm wrong.

-Thanks
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please QuoteYourSources.

I'll move this to the SCJP forum, as this forum is for certification results.
 
Tomek Kaczmarek
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that response depends on type of list declared to use in function
public void addStrings(List list)

When list will be declared for example as:
List<String> list = new ArrayList<String>();
The only way to change it in function is to use:
public void addStrings(List<? super String> list) {


-Tomek
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sujatha M, please check your private messages. You can see them by clicking My Private Messages.
 
sandeep kapse
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sujatha,

I think 'D' is the correct option to choose as Tomek said.

Because, by changing the method signature as per option D,
you are telling compiler that the method can take anything that is
super type of String class. And since you are manipulating the list thats the only way. Then this code will compile without any compilation error.
[ November 09, 2007: Message edited by: Sandeep Misal ]
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's just a badly worded question D) works but E) is the simplest.

The issue it says what 'must you change' to which the answer is you don't have to change anything, I guess.

Bad question in my opinion.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic