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

Discussion

Which line contains the error?

public class Sample80
{
    public static void main(String[] args)
    {
        int[] src = {1, 2, 3};
        int[] dst = new int[2];
        assert src.length == dst.length;
        System.arraycopy(src, 0, dst, 0, src.length);
    }
}

  • A.System.arraycopy(src, 0, dst, 0, src.length);
  • B.int[] dst = new int[2];
  • C.The code is correct as it stands.
  • D.assert src.length == dst.length;

Answer: D

The assertion states something that is plainly false for these arrays, so it fires immediately under -ea and hides the real problem. Without assertions the copy throws ArrayIndexOutOfBoundsException, which is the actual bug: the destination is too small. The check should compare the length being copied against the destination.

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

Post your comment

Please log in or register to post a comment.