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

Discussion

Which line contains the error?

public class Sample94
{
    private static final int RETRIES = 3;

    static int attempt(java.util.Random rng)
    {
        assert true;
        assert RETRIES == 3;
        return rng.nextInt(RETRIES);
    }

    public static void main(String[] args)
    {
        System.out.println(attempt(new java.util.Random(7)));
    }
}

  • A.return rng.nextInt(RETRIES); depends on the assertions having passed
  • B.Both assertions are redundant: neither condition can ever be false.
  • C.assert true; does not compile
  • D.assert RETRIES == 3; does not compile

Answer: B

Both conditions are constants the compiler can evaluate, so neither can ever report anything. They compile and pass, costing a little time under -ea and documenting nothing. An assertion earns its place only when it states something that could conceivably be false.

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

Post your comment

Please log in or register to post a comment.