Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

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

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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).

 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!