6

I am studing for 1Z0-851 Oracla Java SE 1.6 Certification and I saw this question:

Question from test

I marked the first alternative as the correct one and failed! "All of the assert statements are used appropriately" and the answer says that the first one assert(x > 0); is incorrect.. the question is why?

3
  • 1
    The term "used appropriately" is very subjective, hence the confusion. They should clarify that question. Commented Feb 25, 2013 at 17:47
  • Also look at this answer Commented Feb 25, 2013 at 17:47
  • 1
    It would really help if they used a code sample that made even a small amount of sense. Commented Feb 25, 2013 at 17:55

4 Answers 4

4

The correct answer is this

Appropriate and inappropriate use of assertions

You can place an assertion at any location that you don't expect to be reached normally. Assertions can be used to validate the parameters passed to a private method. However, assertions should not be used to validate parameters passed to public methods because a public method must check its arguments regardless of whether assertions are enabled or not. However, you can test postconditions with assertions in both public and non-public methods. Also, assertions should not change the state of a program in any manner.

Src: http://www.freejavaguide.com/java-scjp-part1.pdf

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Nice, didnt know that. Although personally I only ever use assertions in unit tests..!
3

Line 12 is redundant.

if you remove it, the assertion on line 15 will cover the case where x <= 0

To be honest its a strangely worded question but that is all I can see. I am not sure what is meant by appropriately

Comments

1

If you read just the first assert statement -- which should be interpreted as a "precondition" because of its position --, it implies that the function should work properly with any positive int value, which is not true. Therefore, that assertion is misleading.

1 Comment

The function doesn't work for any values of x. Either it's a negative number and fails the first, or it assert falses.
0

Starting by go2, it is easy to understand the assert.
The method does nothing, it just asserts your expectation, that x < 0.

The go method, on the other hand, has a switch.
It is good practice to assert false on the default clause, if you absolutely do not expect your program to fall under this clause, ie, under normal circumstances, one of the cases has to be correct.

The only case on the switch expects x to be exactly 2.
So, to sum up, you don't expect x to be greater than 0, as the first assertion says, you expect x to be 2 and nothing else. Thus, the assertion is not used appropriately.

However, as Jeff noted, the case has no break, which means the default will always be executed, leading, in every scenario, to assert false.

Conclusion: The go method should always result in an error, making assert false properly used, while assert x > 0 isn't correct at all.

5 Comments

Except that the case 2 doesn't have a corresponding break, which means it will always fall through to the assert false as well.
Nicely spotted. I automatically assumed the break.
Me too when I first read it. That's why I had to delete my own answer. Haha
Good spot, I missed it too. My answer just about stands anyway, however the question does seem nonsensical
@Jeff Edited, according to your comment.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.