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

Discussion

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

public class Example07
{
    public static void main(String[] args)
    {
        int used = 250;
        int budget = 100;
        assert used <= budget;
        System.out.println("within budget");
    }
}

  • A.Compilation fails.
  • B.The program prints within budget
  • C.An AssertionError is thrown and within budget is not printed.
  • D.An AssertionError is thrown after done is printed.

Answer: C

With -ea the expression is evaluated, and 250 is not within a budget of 100, so the assertion is false and an AssertionError is thrown at that statement. The error propagates out of main, so the following print never runs. Nothing catches it, so the JVM reports the error and exits with a non-zero status.

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

Post your comment

Please log in or register to post a comment.