File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics" Watch "Generics" New topic


Sujatha Musunuri

Joined: Oct 23, 2007
Posts: 16
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:
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.

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Please QuoteYourSources.

I'll move this to the SCJP forum, as this forum is for certification results.
Tomek Kaczmarek

Joined: Nov 02, 2007
Posts: 4
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) {

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15101

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

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
sandeep kapse

Joined: Jun 07, 2007
Posts: 25
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

Joined: Oct 26, 2003
Posts: 443

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.

"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
I agree. Here's the link:
subject: Generics
It's not a secret anymore!