File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes When assertions are executed 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 "When assertions are executed" Watch "When assertions are executed" New topic
Author

When assertions are executed

Bon Thanh
Greenhorn

Joined: Nov 18, 2009
Posts: 17
This is a question from the ePractice exams for 310-055 purchased directly from Sun Microsystems.

Given:



Which is true?

A - An assertion error will occur at runtime.
B - The command-line invocation java -ea TestAssert will produce no error and the output "more info ".
C - The command-line invocation java -ea TestAssert will produce no error and the output "after assert ".
D - The command-line invocation java -ea TestAssert will produce no error and the output "more info after assert ".
E - The command-line invocation java -ea TestAssert will produce no error and the output "after assert more info ".

Sun's answer is:
Option A is correct. The command-line enables assertions, and more info is added to the stack trace before the program ends at line 3 with an AssertionError.

I disagree. Assertions are disabled by default at runtime. The question did not provide the command-line being used. I think the correct answer should be (which is not one of the options provided):
The command-line invocation java -ea TestAssert will produce an assertion error and the output "more info ".

Can someone please confirm?

Thank you!
Bonnie
Dejan Miler
Ranch Hand

Joined: Nov 14, 2009
Posts: 56
I agree whit you my friend.
This question is little misleading.
But let me ask you something.
When this error will occur ?

The command-line invocation java -ea TestAssert will produce an assertion error and the output "more info ".

At runtime or during compiling time ?
Of course at runtime.
Then first logical answer is A !

Dejan.


SCJP 1.6 in progress ....
Bon Thanh
Greenhorn

Joined: Nov 18, 2009
Posts: 17
Well, Dejan, if you put it that way, then I agree that the logical answer out of the answers provided would be A.
Thanks!
S Bhanage
Greenhorn

Joined: Oct 28, 2009
Posts: 13
Option A is the only possible answer.

Without "-ea" option, the assertion error, if any, is suppressed.
But with "-ea" it is displayed on the console.


Hence output is different with different option of executing the code.
But in any case choice A is always going to happen.

So Option A is correct answer.
Bon Thanh
Greenhorn

Joined: Nov 18, 2009
Posts: 17
Interesting. I thought disabling assertions meant that they weren't even processed. Since when assertions are enabled, your application's performance is affected.
So you're saying that enabling or disabling assertions just means whether or not they are displayed on the console? But assertion statements are always processed?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When assertions are executed