Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B trick question

 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • 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
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • 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: 423
  • Mark post as helpful
  • send pies
  • 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
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • 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: 423
  • Mark post as helpful
  • send pies
  • 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!
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • 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: 423
  • Mark post as helpful
  • send pies
  • 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>>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic