• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A assertions qustion help about Man's mock!

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you all!!!
the sky becomes clear!
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so if an assertion is false, an AssertionError is thrown.
reply
    Bookmark Topic Watch Topic
  • New Topic