• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

K&B trick question

 
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!



The question is:
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?

One of the correct options is remove the cast from Object to Integer:
Integer total = accountTotals.get(accountName); // error: incompatible types

I agree that the code make use of appropriate generic types, but this is a very trick question.

Are there questions like this in the exam
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Are there questions like this in the exam



Yes.

But why is this a trick question. When generics is applied to the map the return value would automatically be an Integer thereby not needing a cast
 
Leandro Coutinho
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Bala wrote:

Are there questions like this in the exam



Yes.

But why is this a trick question. When generics is applied to the map the return value would automatically be an Integer thereby not needing a cast


Because it just changed line 5. It didn't change line 2 to be generic. So the code does not compile, but the question didn't ask this...
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Leandro Coutinho wrote:

Deepak Bala wrote:

Are there questions like this in the exam



Yes.

But why is this a trick question. When generics is applied to the map the return value would automatically be an Integer thereby not needing a cast


Because it just changed line 5. It didn't change line 2 to be generic. So the code does not compile, but the question didn't ask this...



The question probably assumes that you would change Map to be a generic ? After all the class internals are updated to be generic. I do not have the question at hand to scrutinize this more. Can you post the entire question ?
 
Leandro Coutinho
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Bala wrote:The question probably assumes that you would change Map to be a generic ? After all the class internals are updated to be generic.


I realized what I missed. It's all the correct options combined.
It is still a trick question, because we are used to think in the options independently, and the question is not very clear (at least for me), but it is reasonable:
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.)


Does the exam make it clear when the correct options must be combined?

Thank you Deepak!
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well "clear" is a somewhat subjective term, but typically the real exam will include phrases like:

"which fragment, insert independently..."

or

"which three inserted together..."

In this case however it's best to include the entire question when you want to discuss a question. To some degree the portion of the question that you posted doesn't provide every detail that might be necessary to properly discuss it. No matter whose mock question you're discussing, it;s dangerous to edit the question when you post it - often the devil will be in a small detail you didn't consider.

Thanks!

Bert
 
Leandro Coutinho
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Bert!

Question 5. Chapter 7: Generics and Collections.



B, E, and G are correct.

* It automatically closes the angle bracket. There are no right angle brackets before <Integer>>
 
reply
    Bookmark Topic Watch Topic
  • New Topic