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

Discussion

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

public class Fragment06
{
    public static void main(String[] args)
    {
        int x = 1;
        assert x == 1;
        assert x == 2;
        System.out.println("both passed");
    }
}

  • A.Compilation fails.
  • B.The program prints both passed
  • C.An AssertionError is thrown on the second assert.
  • D.No output is produced.

Answer: B

Both assertions are inert without -ea, including the false one, so control reaches the print. Note that the first assertion being true is irrelevant: it was not evaluated either. Under -ea the program would throw an AssertionError at the second statement and print nothing.

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

Post your comment

Please log in or register to post a comment.