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

Discussion

Which line is an inappropriate use of an assertion?

public class Sample13
{
    public static void main(String[] args)
    {
        assert args.length == 2 : "usage: Sample13 <in> <out>";
        String in = args[0];
        String out = args[1];
        System.out.println(in + " -> " + out);
    }
}

  • A.assert args.length == 2 : "usage: Sample13 ";
  • B.String in = args[0];
  • C.System.out.println(in + " -> " + out);
  • D.Nothing here is inappropriate.

Answer: A

Command-line arguments come from outside the program, so validating them is part of its contract with the user, not an internal invariant. Without -ea the check vanishes and the next line throws ArrayIndexOutOfBoundsException instead of reporting the usage. Input validation needs an explicit test.

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

Post your comment

Please log in or register to post a comment.