• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Generics K&B Page 638 Question 5 Code

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you done step B yet?
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought I need to insert the 3 options independently..Thanks Duc
 
Ranch Hand
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again Dennis, thank you. Its something simple that I didnt know as I had never tried it in practice.
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic