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 A assertions  qustion help about Man's mock! 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 "A assertions  qustion help about Man Watch "A assertions  qustion help about Man New topic
Author

A assertions qustion help about Man's mock!

Erico Doon
Greenhorn

Joined: Feb 15, 2003
Posts: 15

Which statements are true?
a. With assertions enabled it prints "ABC" followed by an AssertionError message.
b. With assertions enabled it prints "ABCE" followed by an AssertionError message.
c. With assertions disabled it prints "ABC"
d. With assertions disabled it prints "ABCE"
e. Assertions should not be used within the default case of a switch statement.
f. A compiler error is generated.
g. None of the above.
//save as: C.java and compile it:
//C.java:8: warning: as of release 1.4, assert is a keyword, and may not be used a
s an identifier
assert false;
^
C.java:8: not a statement
assert false;
^
C.java:8: ';' expected
assert false;
^
2 errors
1 warning
The correct answer is A ,D, but why?
Thank you for your help!!

[Dan added formatting.]
[ February 27, 2003: Message edited by: Dan Chisholm ]

I want to pass SCJP for 1.4 !I am very grateful to you for helping me!!
John Paverd
Ranch Hand

Joined: Nov 17, 2002
Posts: 115
You must use a special switch to compile code that contains assertions:
javac -source 1.4 C.java
In future versions of Java assertions may be enabled by default, but as of JDK 1.4 you must explicitly enable the compilation of assertions. This was done to allow code that compiled under previous versions of the JDK to compile under JDK 1.4.
[ February 27, 2003: Message edited by: John Paverd ]

SCJP 1.4
Erico Doon
Greenhorn

Joined: Feb 15, 2003
Posts: 15
Thank you ,John Paverd!!
but i am still confused.
I try :
javac -source 1.4 C.java
//compile success
java C
//output: ABCE
Why the output is the same as that without no statement(assert false ?
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Erico,
To run the program with assertions enabled you will need to use the switch -ea.
java -ea C


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
John Paverd
Ranch Hand

Joined: Nov 17, 2002
Posts: 115
Erico
Here is a link that explains in more detail the information that Dan and I gave to you: Sun Java documentation on assertions
Erico Doon
Greenhorn

Joined: Feb 15, 2003
Posts: 15
thank you all!!!
the sky becomes clear!
Giri Mandalika
Greenhorn

Joined: Jan 25, 2003
Posts: 5
One more link for very good material on assertions:
http://www.ii.uib.no/~khalid/pgjc/jcbook/JC2_Ch05-assertions-excerpt.pdf
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
so if an assertion is false, an AssertionError is thrown.


giddee up
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A assertions qustion help about Man's mock!
 
Similar Threads
Assertion
For Dan's attention:
Dan's Q of Assertion?
dan exam doubt 11
assertions doubt