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 2 of 2.
Tul baghresh said:
7 years ago
How condition x>0 is true?
Tarang Malvaniya said:
9 years ago
Output is A) finished.
Pragya sharma said:
1 decade ago
Thanks.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers