wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics K&B Page 638 Question 5 Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics K&B Page 638 Question 5 Code" Watch "Generics K&B Page 638 Question 5 Code" New topic
Author

Generics K&B Page 638 Question 5 Code

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi, Here is the code from Generics K&B Page 638 Question 5
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.)
Correct answers are
B. Replace line 13 with
private Map<String, Integer> accountTotals = new HashMap<String, Integer>();
E. Replace lines 17–20 with
Integer total = accountTotals.get(accountName); //Type mismatch: cannot convert from Object to Integer
if (total == null) total = 0;
return total;

How come Option "E" is correct..I am getting Type mismatch: cannot convert from Object to Integer error..Please advice


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Duc Vo
Ranch Hand

Joined: Nov 20, 2008
Posts: 254
Have you done step B yet?

“Everything should be as simple as it is, but not simpler.” Albert Einstein
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
I did not do it because question is not clear. If I keep B step, I will not get error for Option E..Please let me know why should I keep Step "B"
Duc Vo wrote:Have you done step B yet?
Duc Vo
Ranch Hand

Joined: Nov 20, 2008
Posts: 254
It's actually quite clear. From the question:

Which of these steps could be performed? (Choose three.)

Which means you need to perform all three steps.
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
I thought I need to insert the 3 options independently..Thanks Duc
Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

Unrelated query on the same question:

The book says:

"D is wrong because an int cannot autobox to null".

FYI - option d:



I accept that this is correct as we are unboxing an Integer to an int. However, I was not aware that this cannot be null. can somebody please explain why. Is this true for all unboxing?


OCPJP 6, OCMJD
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Glen Iris wrote:Unrelated query on the same question:

The book says:

"D is wrong because an int cannot autobox to null".

FYI - option d:



I accept that this is correct as we are unboxing an Integer to an int. However, I was not aware that this cannot be null. can somebody please explain why. Is this true for all unboxing?


An Integer can be null, but not an int. If we attempt an autoboxing assignment using a null reference, an exception will be thrown. This is true for all wrappers.
Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

Again Dennis, thank you. Its something simple that I didnt know as I had never tried it in practice.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8875
    
    5
There is an informal standard on the real exam that we try to follow in the book:

If the question wants you to try only one answer at a time, like inserting a line of code, it should say something like:

"Which fragment, inserted independently at line X, compiles?"

The key phrase to look for is the "inserted independently". This isn't always present, but it usually is.

hth,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics K&B Page 638 Question 5 Code