Java Programming - Assertions - Discussion

Discussion Forum : Assertions - Finding the output (Q.No. 4)
What will be the output of the program (when you run with the -ea option) ?
public class Test 
    public static void main(String[] args) 
        int x = 0;  
        assert (x > 0) : "assertion failed"; /* Line 6 */
Compilation fails.
An AssertionError is thrown.
An AssertionError is thrown and finished is output.
Answer: Option

An assertion Error is thrown as normal giving the output "assertion failed". The word "finished" is not printed (ensure you run with the -ea option)

Assertion failures are generally labeled in the stack trace with the file and line number from which they were thrown, and also in this case with the error's detail message "assertion failed". The detail message is supplied by the assert statement in line 6.

13 comments Page 1 of 2.

Vikash Bhel said:   4 years ago

And thus x is not greater than 0. So the condition is false.
So, how the answer is correct?

Tul baghresh said:   4 years ago
How condition x>0 is true?

Harsh said:   6 years ago
How to enable ea option in Net bean?

Tarang Malvaniya said:   6 years ago
Output is A) finished.

Dahaf said:   8 years ago
Assertions help a programmer to declare his assumptions.

Manpreet said:   9 years ago
I don't understand the use of assertion in java.

Kds said:   10 years ago
I was running it from eclipse and got finished as answer.

Shwets said:   10 years ago
Could you please explain what is the use of assertion, why to use assertion?

Suyash said:   1 decade ago

If we want to execute assertion line then.

With the interpreter we should use -ea to execute assertion line of code.

Vivek kumar said:   1 decade ago
What is -ea option? & what does -ea do?. mean javac is for compilation & java is for interpretation.

Post your comments here:

Your comments will be displayed after verification.