Machine-translated by Google. Questions are authored in English.
Top

Discussion

The program is run with java Sample01 - what will be the output?

public class Sample01
{
    public static void main(String[] args)
    {
        int x = 5;
        assert x > 10;
        System.out.println("done");
    }
}

  • A.The program prints done
  • B.An AssertionError is thrown at runtime.
  • C.Compilation fails.
  • D.No output is produced.

Answer: A

Assertions are disabled by default, and a disabled assertion is semantically equivalent to an empty statement. The expression x > 10 is never evaluated, so no AssertionError can be thrown and the program falls through to the print. Only java -ea Sample01 would make this fail.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.