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

Discussion

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

public class Probe38
{
    public static void main(String[] args)
    {
        for (int i = 0; i < 5; i++)
        {
            assert i < 3 : i;
            System.out.print(i);
        }
    }
}

  • A.The program prints 01234
  • B.012, then an AssertionError with message 3
  • C.The program prints 012
  • D.An AssertionError with message 0

Answer: B

The assertion is checked at the top of each iteration. It passes for i equal to 0, 1 and 2, each of which is printed, and fails when i reaches 3. The error is thrown before that iteration's print, so the visible output is 012 followed by an AssertionError whose detail message is 3.

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

Post your comment

Please log in or register to post a comment.