Which command causes the following class to throw an AssertionError at runtime?.
public class Main { private static final short V = 12; private Main() { super(); /*from w ww .java 2 s . co m*/ } int checkHour() { assert V > 12; return V; } public static void main(String... ticks) { new Main().checkHour(); } }
A. java -da Main
B. java -ea:Main -da Main
C. java -ea -da:Main Main
D. java -enableassert Main
E. None of the above since the Main class does not compile.
B.
The code compiles without issue, making Option E incorrect.
Option A is incorrect because it disables all assertions, which is the default JVM behavior.
Option B is the correct answer.
It disables assertions everywhere but enables them within the Main class, triggering an AssertionError at runtime within the checkHour()
method.
Option C is incorrect because it enables assertions everywhere but disables them within the Main class.
Option D is also incorrect because -enableassert
is not a valid JVM flag.
The only valid flags to enable assertions are -ea and -enableassertions
.