Java Programming - Assertions - Discussion
Discussion Forum : Assertions - Finding the output (Q.No. 4)
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 */
System.out.println("finished");
}
}
Answer: Option
Explanation:
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.
Discussion:
13 comments Page 1 of 2.
Venkata said:
1 decade ago
"finished" is output
Sundar said:
1 decade ago
@All
The given answer is correct only. Don't forget to add -ea option while executing the java code.
I have got the following output:
D:\Java>javac Test.java
D:\Java>java -ea Test
Exception in thread "main" java.lang.AssertionError: assertion failed
at Test.main(Test.java:6)
D:\Java>java Test
finished
I hope the above output will clear your doubts. Have a nice day!
The given answer is correct only. Don't forget to add -ea option while executing the java code.
I have got the following output:
D:\Java>javac Test.java
D:\Java>java -ea Test
Exception in thread "main" java.lang.AssertionError: assertion failed
at Test.main(Test.java:6)
D:\Java>java Test
finished
I hope the above output will clear your doubts. Have a nice day!
Pragya sharma said:
1 decade ago
Thanks.
Vivek kumar said:
1 decade ago
What is -ea option? & what does -ea do?. mean javac is for compilation & java is for interpretation.
Suyash said:
1 decade ago
@Vivek.
If we want to execute assertion line then.
With the interpreter we should use -ea to execute assertion line of code.
If we want to execute assertion line then.
With the interpreter we should use -ea to execute assertion line of code.
Shwets said:
1 decade ago
Could you please explain what is the use of assertion, why to use assertion?
Kds said:
1 decade ago
I was running it from eclipse and got finished as answer.
Manpreet said:
1 decade ago
I don't understand the use of assertion in java.
Dahaf said:
1 decade ago
Assertions help a programmer to declare his assumptions.
Tarang Malvaniya said:
9 years ago
Output is A) finished.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers