• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusing Question on Assert

 
Sergei Iakhnin
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This from whizlabs practice test for 1.4

Which of the following are true about assertions:

A. Assertions are by default enabled in JDK 1.4 during compilation and runtime.

B. In assert syntax - assert expression1; the expression1 must be a boolean expression

C. In assert syntax - assert expression1; the expression1 must be an expression resulting in an int

D. An assert statement may not be enclosed in a try-catch block

E. Only option B is correct


My answer was B and E while their answer is B and D. The following explanation is given:


If an assertion statement fails it trhows an AssertionError which is a subclass of Error so it may not be enclosed in a try catch block. Hence option D is correct.

Please note that it is perfectly valid to enclose an assert statement in a try-catch block and catch an AssertionError, but it is not mandatory.


So would someone be so kind to explain to me how you "may not do it" and it is "perfectly valid" at the same time?

Also, Is it the general sentiment that the Whizlabs final is considerably harder than the whizlabs practice test?

Thankful in advance.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is possible that the person who wrote this question does not have English as a first language. For option "D" I think the words "should not" should have been used in place of "may not". It is perfectly possible, and you may want to, catch an AssertionError. But in most cases you should not do so.
 
Sergei Iakhnin
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you are right. They might have been trying to say "Wrapping an assert statement in a try-catch block is optional" but instead staid "An assert statement cannot be enclosed in a try-catch block". I would hate to encounter something like that on a real exam.
 
Philip Heller
author
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that only option B is correct.

That means option E is also correct.

That means option E is not correct, since it says only B is correct.

Oops, that means now E is correct again.

Etc etc forever. Did you hear about the programmer who starved to death in the shower? The shampoo bottle said, "Lather, rinse, repeat ..."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic