It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusing Question on Assert Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confusing Question on Assert" Watch "Confusing Question on Assert" New topic
Author

Confusing Question on Assert

Sergei Iakhnin
Ranch Hand

Joined: Nov 09, 2004
Posts: 53
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.


SCJP 1.4, SCWCD 1.4
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Sergei Iakhnin
Ranch Hand

Joined: Nov 09, 2004
Posts: 53
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

Joined: Oct 24, 2000
Posts: 119
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 ..."


Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
 
wood burning stoves
 
subject: Confusing Question on Assert
 
Similar Threads
assertions in try-catch blocks
Section 2 mock test questions
Exam Objectives Flow Control.
Exception
Assertions Problem