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 assert help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "assert help" Watch "assert help" New topic
Author

assert help

PETER CARTER
Ranch Hand

Joined: Aug 28, 2004
Posts: 70
public class Test {
2. public static void main(String[] args) {
3. int x = 0;
4. assert (x > 0): �assertion failed�;
5. System.out.println(�finished�);
6. }
7. }
A. finished
B. Compilation fails.
C. An AssertionError is thrown.
D. An AssertionError is thrown and finished is out0put.
The answer: A.
Why?How to use "assert" ??
Thanks !
[ September 11, 2004: Message edited by: Barry Gaunt ]
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
Hi Peter,

You forgot to mention the question (although we can infer that from the answer given), was the program run with assertions enabled/disabled?

For more on Assertions, check the documentation provided by Sun.

[Edit] Read the intro and usage for your answer, it will take just 5 minutes (time well spent).
Sheldon
[ September 10, 2004: Message edited by: Sheldon Fernandes ]
Piyush Jain
Ranch Hand

Joined: Apr 25, 2003
Posts: 60
Answer is A because assertion is disabled by default unless it is enabled during compilation and runtime. so the assert statement is just ignored by the compiler.

if assertion was enabled, assertion error followed by assertion failed will be the output, since x >0 fails.
PETER CARTER
Ranch Hand

Joined: Aug 28, 2004
Posts: 70
javac -source 1.4 Test.java
The answer is C;
assertion is disabled by default
The answer is A.

Thanks !!
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
javac -source 1.4 Test.java
Tells the javac compiler to accept code containing assertions.

java Test
The answer is A

java -ea:Test Test OR java -ea Test
The answer is C

Sheldon
[ September 11, 2004: Message edited by: Sheldon Fernandes ]
PETER CARTER
Ranch Hand

Joined: Aug 28, 2004
Posts: 70
I konw ,thanks !!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: assert help
 
Similar Threads
Assertions mock exam question
on assertions
Enabling Assertions
My problem wif Java questionssss
Assertion enable or not?