• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assert help

 
PETER CARTER
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javac -source 1.4 Test.java
The answer is C;
assertion is disabled by default
The answer is A.

Thanks !!
 
Sheldon Fernandes
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I konw ,thanks !!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic