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

Discussion

Which line contains the error?

public class Sample65
{
    public static void main(String[] args)
    {
        assert check(2) : "even";
        assert !check(2) : "odd";
        System.out.println("consistent");
    }

    static boolean check(int n) { return n % 2 == 0; }
}

  • A.assert !check(2) : "odd";
  • B.assert check(2) : "even";
  • C.static boolean check(int n) { return n % 2 == 0; }
  • D.Nothing here is wrong.

Answer: A

The two assertions contradict each other, so one must always fail whatever check() returns. The first passes for 2 and the second then throws. A pair like this usually survives an edit where a condition was negated in one place and not the other; the code can never satisfy both.

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

Post your comment

Please log in or register to post a comment.