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 Doubt-Generics and Collections-Kathy Sierrra- SCJP6-Question5 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 "Doubt-Generics and Collections-Kathy Sierrra- SCJP6-Question5" Watch "Doubt-Generics and Collections-Kathy Sierrra- SCJP6-Question5" New topic
Author

Doubt-Generics and Collections-Kathy Sierrra- SCJP6-Question5

Richa Sharma
Ranch Hand

Joined: Dec 06, 2008
Posts: 47
I have a doubt in Q.5 Chapter-Generics and Collections of Kathy Sierrra book for SCJP 6.

Question:


This class is to be updated to make use of appropriate generic types, with no changes in
behavior (for better or worse). Which of these steps could be performed? (Choose three.)

A. Replace line 13 with
private Map<String, int> accountTotals = new HashMap<String, int>();

B. Replace line 13 with
private Map<String, Integer> accountTotals = new HashMap<String, Integer>();

C. Replace line 13 with
private Map<String><Integer>> accountTotals = new HashMap<String><Integer>>();

D. Replace lines 17–20 with
int total = accountTotals.get(accountName);
if (total == null) total = 0;
return total;

E. Replace lines 17–20 with
Integer total = accountTotals.get(accountName);
if (total == null) total = 0;
return total;

F. Replace lines 17–20 with
return accountTotals.get(accountName);

G. Replace line 24 with
accountTotals.put(accountName, amount);

H. Replace line 24 with
accountTotals.put(accountName, amount.intValue());

Correct Answers are B,E,G

My doubt is why is choice D incorrect.
vijay saraf
Ranch Hand

Joined: Jan 08, 2005
Posts: 141
D.Replace lines 17–20 with
int total = accountTotals.get(accountName);

E. Replace lines 17–20 with
Integer total = accountTotals.get(accountName);


Look accountTotals would return a Integer Object,
If you choose D a boxing-unboxing will happened.

Question said
with no changes in behavior (for better or worse).


Thanks
Vijay Saraf.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt-Generics and Collections-Kathy Sierrra- SCJP6-Question5