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

Discussion

Which line is an inappropriate use of an assertion?

public class Sample59
{
    static int made = 0;

    Sample59()
    {
        assert ++made > 0;
    }

    public static void main(String[] args)
    {
        new Sample59();
        new Sample59();
        System.out.println(made);
    }
}

  • A.System.out.println(made);
  • B.Every line above is correct as written.
  • C.assert ++made > 0;
  • D.static int made = 0;

Answer: C

The counter is incremented inside the assertion, so it records constructions only when -ea is set: the program prints 2 with assertions on and 0 with them off. A count the program reports is state it depends on, and must be maintained by an ordinary statement.

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

Post your comment

Please log in or register to post a comment.