wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B trick question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B trick question" Watch "K&B trick question" New topic
Author

K&B trick question

Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
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

Joined: Feb 24, 2006
Posts: 6661
    
    5

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


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
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

Joined: Feb 24, 2006
Posts: 6661
    
    5

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

Joined: Mar 04, 2009
Posts: 417
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

Joined: Oct 14, 2002
Posts: 8803
    
    5
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


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
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>>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: K&B trick question
 
Similar Threads
AutoBoxing/Unboxing
Doubt-Generics and Collections-Kathy Sierrra- SCJP6-Question5
Generics K&B Page 638 Question 5 Code
Doubt in K&B SCJP 6: Ch 7, Q 5
Regarding phrasing of questions...