• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on Assertion?

 
Seenu Reddy
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am new to Assertion topic. Could you please answer my question and explain?

Assuming assertions are enabled, which of these assertion statement throw an error?
a) assert true : true;
b) assert true : false;
c) assert false : true;
d) assert false : false;

My ans : c, d
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct, the answers should be c and d, and they are easy to verify by writing a test program.

For assertion, there are 2 forms:
1. assert expression1
2. assert expression1 : expression2

expression 1 must result in a boolean expression. Whenever expression1 is false, AssertError will be thrown. Thus, c and d give AssertError.

expression2 is the message that you want to pass to the AssertError. Even you pass in a boolean value, it will be converted to String, and printed out.


Exception in thread "main" java.lang.AssertionError: false
at AssertTest.main(AssertTest.java:72)


Details can be found from:
http://www.javaworld.com/javaworld/jw-11-2001/jw-1109-assert.html

Nick
 
Seenu Reddy
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick,
Thanks for your response and explanation.

Regards,
Seenu
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic