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

Discussion

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

public class Demo39
{
    public static void main(String[] args)
    {
        int[] scores = {90, 80, -1, 70};
        for (int s : scores)
        {
            assert s >= 0 : s;
            System.out.print(s + " ");
        }
    }
}

  • A.No output is produced.
  • B.The program prints 90 80
  • C.The program prints 90 80 -1 70
  • D.An AssertionError is thrown on the third element.

Answer: C

With assertions disabled the check is skipped on every pass, so the negative score is never objected to and the loop prints all four values. Under -ea the same program would stop at the -1 and throw with that value as its message. A loop is where a cheap assertion earns its keep, because it runs on every element.

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

Post your comment

Please log in or register to post a comment.