File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertions 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 "Assertions" Watch "Assertions" New topic


Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 528

First off, if a question were to simply ask "What would be the result?" on a question involving assterions on the exam, do we assume that assertions are enabled or disabled at runtime? Compiled with or not compiled with?


What is the result of the following question?

A. An AssertionError is thrown and "Continue..." is output.
B. An AssertionError is thrown with the message �No good�.
C. An AssertionError is thrown with the message �Ok�.
D. Continue...
E. Compilation fails.

Answer givenis E.
I also think it won't compile due to incorrect syntax on line 1???
But i don't trust this mock's results because they have alot of errors in their answers.


Live life to an interface, not an implementation!
Richard Martin

Joined: Jun 06, 2005
Posts: 2
assert statements can take 2 forms

Ex 1: assert (boolean expression);


Ex 2: assert (boolean expression) : "houston we have a problem"; -- (in this example the second part can be anything that returns a value, you can send it of to a method as long as the method returns a value, the result will be converted to a string and displayed with the stack trace)

The reason this code will fail to compile is because (val > 0) ? �Ok� : �No good� will not result in a boolean value, it will result in a String. The first part of an assert statement must always result in a boolean value, if you have a second expression as in example 2 that must always result in a value.

The first question is a good one though, from a similar point if they use assert as a variable name in a question are we to assume assertions are enabled?
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 528

So yor saying the following will compile fine:

assert (val > 0) ? true : false;

I agree. Here's the link:
subject: Assertions
It's not a secret anymore!