Welcome Guest | Sign in | Register
Assertions - Discussion Page For Q.2526 | Java Programming Questions and Answers | Java Programming Free Online-Tests | LucentBlackBoard | LucentBlackBoard.com

Home > Java Programming > Assertions > Questions and Answers

Q: 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");
}
}
A. finished B. Compilation fails.
C. An AssertionError is thrown. D. An AssertionError is thrown and finished is output.

Answer and Explanation

Answer:An AssertionError is thrown.

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 labelled 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.

Comment on this Question

Name:

Loading...




Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.