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

Discussion

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

public class Case58
{
    static boolean check(int n)
    {
        return n > 0;
    }

    public static void main(String[] args)
    {
        assert check(1) : "one";
        assert check(-1) : "minus one";
        System.out.println("end");
    }
}

  • A.The program prints end
  • B.An AssertionError with message minus one
  • C.An AssertionError with message one
  • D.Compilation fails.

Answer: B

Assertions are evaluated in order. The first passes because check(1) returns true, so execution moves on. The second fails and throws with its own detail message, minus one, and the print is never reached. Only the first failing assertion is reported.

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

Post your comment

Please log in or register to post a comment.