This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes some odd answers from the K&B CD - part 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "some odd answers from the K&B CD - part 2" Watch "some odd answers from the K&B CD - part 2" New topic
Author

some odd answers from the K&B CD - part 2

John Schubert
Ranch Hand

Joined: Sep 21, 2011
Posts: 39

Yea, I know, here it comes uncle John with his daily rant XD. Actually, I've started this series to get the opinion of more expert people, and to help me memorising the answers.

This is a question from K&B CD:
Which are true? (Choose all that apply):
-...<some options here>
-Use assertions to verify the arguments of private method.
-Never throw an AssertionError explicitly.


The first thing that is a bit odd here is that you have to determine if "use assertion" is true, or "never throw" is true. These are commands or advices, rather than statements. For those whose native language is not English (like me) it is kind of difficult to know if they are asking if some thing is possible or if that thing is - or not - good practise. Let's assume that we have to choose if they are good practise or not.

Use assertions to verify the arguments of private method.

This means having a method like this:


But I think this would be better:


Why? because Assertions have to be enabled to work, and some programmers may just forget it, or they may not be aware of the asserts. The exception will work always.

About the second:
Never throw an AssertionError explicitly.

Throwing Errors (and this one in particular) is certainly weird. Yet it is legal, and there are very serious projects out there where it is done. Here you are a couple of examples:
a test from Guava
a class in vanilla-java

I'm starting to think that it doesn't matter how many of these questions I fail in mock tests, there'll be always place for another one


Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8804
    
    5
Hey John,

There are objectives in the exam that were designed to test the candidate's knowledge of what Sun Microsystems considered "best practices". I think that Oracle will more or less take similar positions. So, it's not so much that you could find counter examples, it's more that you are a candidate for a particular company's (in this case Oracle's) certification. So the point is to be familiar with THEIR standards. Doesn't mean you might not choose to break those standards sometimes, but you should be AWARE of these standards.

hth,

Bert


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

Joined: Sep 21, 2011
Posts: 39

Hi Bert! Nice to read you here.
Great book, it really helps.

I agree with you. Not sure about Oracle, but Sun's standars have always been mine's too. What happens is that I tend to learn things reasoning them first, and there are some questions where my instinct tells me to do a very different thing. For instance, the validation with assertions, I rarely see them in real world code. People use to throw exceptions, and keep other complex validations in jUnit tests.

dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Bert Bates wrote:Hey John,

There are objectives in the exam that were designed to test the candidate's knowledge of what Sun Microsystems considered "best practices". I think that Oracle will more or less take similar positions. So, it's not so much that you could find counter examples, it's more that you are a candidate for a particular company's (in this case Oracle's) certification. So the point is to be familiar with THEIR standards. Doesn't mean you might not choose to break those standards sometimes, but you should be AWARE of these standards.

hth,

Bert


This doesn't address the problem that the options are written in the form of imperatives, which can not evaluate to true or false.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8804
    
    5
Hi Dennis,

A couple of things to keep in mind when you read mock exam questions:

1 - Mock exam authors should try to follow the tone and language of the real exams as much as possible.
2 - The language on the real exam is a bit stilted due to translation requirements and limits of the world-wide-consistent testing center tools.
3 - The creators of the real exam work within strict guidelines as far as formatting of questions.

The bottom line is, you gotta get used to the style.

hth,

Bert
Sam Hazim
Greenhorn

Joined: Jul 19, 2011
Posts: 26
Out of interest, what's the answers here?

I've read that you should never use Assertions on public methods (as you have less control over who will make use of the public method). So I believe using Assertions on private methods is good practice.

I've also read (K&B) that you should never try and CATCH AssertionError, so my guess is that you should never explicitly throw one either (but I'm not 100% sure).

Anyone disagree?

Thanks,
 
 
subject: some odd answers from the K&B CD - part 2
 
Similar Threads
Question on Assertion from KnB's CD
uestion on Assertion
Error Throwing Question
Throw an AssertionError explicity?
Master exam question on assertion