Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A assertions qustion help about Man's mock!

 
Erico Doon
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
John Paverd
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Erico Doon
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Erico,
To run the program with assertions enabled you will need to use the switch -ea.
java -ea C
 
John Paverd
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you all!!!
the sky becomes clear!
 
Giri Mandalika
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so if an assertion is false, an AssertionError is thrown.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic