Tuesday, August 24, 2010

Assertions: Bold.java

Courtesy of Sun Web Learning Center


 7. class Bold {
 8.   public static void main(String[] args) {
 9.     Boolean boo = true;
10.     assert(!boo): "yee ";
11.     System.out.println("haw ");
12.   }
13. }

And the command line invocation:

java -ea Bold

What is the result?

A yee with no errors
B haw with no errors
C yee haw with no errors
D An Assertion error including yee in the output
E An Assertion error including haw in the output
F An Assertion error with neither yee or haw in the output

D is correct. The command-line enables assertions, and yee is added to the stack trace before the program ends with an AssertionError.

No comments:

Post a Comment