The following class was launched with a -ea key. What is the result of following code execution?

public class Test {
    public static void main(String [] args) {
        Boolean b = true;
        int i = 10;
        String str = "Hello World";
        assert (b == true) : "true";
        assert (i != 10) : "false";
Run-time error.
AssertionError exception will be thrown because i != 10 condition is false

Enabling and Disabling Assertions
By default, assertions are disabled at runtime. Two command-line switches allow you to selectively enable or disable assertions.

To enable assertions at various granularities, use the -enableassertions, or -ea, switch. To disable assertions at various granularities, use the -disableassertions, or -da, switch.

See documentation

Also, updated the explanation.

2017 Oct 27, 3:26:27 PM

Hey, guys! Try to run in this way: 1) open terminal create file with the code in the question; 2) compile with command `javac`; 3) run command `java Test` - it will work; 4) run command `java -ea Test` it will fail with next error: Exception in thread "main" java.lang.AssertionError: false at Test.main(

2017 Oct 27, 3:25:50 PM

it's print - Hello World.

2017 Jun 16, 1:30:38 PM

I haven`t run-time error. Its printed "Hello World"

2017 May 26, 6:08:29 PM

prints Hello world for me

2017 Jan 9, 11:12:19 AM

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Sign Up Now
or Subscribe for future quizzes