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";
        System.out.println(str);
    }
}
Explanation
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 http://docs.oracle.com/javase/7/docs/technotes/guides/language/assert.html

Also, updated the explanation.

2017 Oct 27, 3:26:27 PM

Hey, guys! Try to run in this way: 1) open terminal create Test.java file with the code in the question; 2) compile with command `javac Test.java`; 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(Test.java:7)

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

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Продовжуйте вивчати
тести з Java
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести