Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

on assertions

 
velan vel
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai
here bydefaultly the assertions are disabled
so it should print finished
but why this code is giveing compile time error

Given:
1. public class Test {
2. public static void main(String[] args) {
3. int x = 0;
4. assert (x > 0) ? "assertion failed" : "assertion passed";
5. System.out.println("Finished");
6. }
7. }
What is the result?
A. finished
B. Compilation fails.
C. An AssertionError is thrown and finished is output.
D. An AssertionError is thrown with the message "assertion failed".
E. An AssertionError is thrown with the message "assertion passed".
Answer: B

by
velan vel
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
assert (x > 0) ? "assertion failed" : "assertion passed";

the two syantaxes for assert statments are

1. assert expression1;
2. assert expression1:expression2;

u r using something like (but not exactly) case 2. the expression is incorrect...and does not exqactly return boolean value

in ur case < (x>0) ? "assertion failed" ) turns out to be expression 1
and it doesnt return a boolean

hence compiler error
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic