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

Discussion

Which line contains the error?

public class Sample24
{
    static int parse(String s)
    {
        assert s != null;
        return Integer.parseInt(s);
    }

    public static void main(String[] args)
    {
        System.out.println(parse(null));
    }
}

  • A.assert s != null; fails to compile
  • B.parse(null) cannot pass null to an int parameter
  • C.Nothing is wrong; the program prints 0.
  • D.The assertion is skipped without -ea, so Integer.parseInt(null) throws instead

Answer: D

parse is not public, so asserting the precondition is reasonable. The fault is the caller: main knowingly passes null, which is a bug the assertion only reports when -ea is set. Without it Integer.parseInt(null) throws NumberFormatException, which names the symptom rather than the cause.

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

Post your comment

Please log in or register to post a comment.