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

Discussion

What will be the output of the program?

public class Listing37
{
    Listing37()
    {
        int[] src = {1, 2, 3, 4};
        int[] dst = new int[4];
        System.arraycopy(src, 1, dst, 0, 2);
        System.out.println(dst[0] + " " + dst[1] + " " + dst[2]);
    }

    public static void main(String[] args)
    {
        new Listing37();
    }
}

  • A.The program prints 2 3 0
  • B.The program prints 2 3 4
  • C.The program prints 0 2 3
  • D.The program prints 1 2 0

Answer: A

The call copies 2 elements starting at index 1 of the source into index 0 of the destination, so 2 and 3 land in the first two slots. Everything else keeps the default value for an int array, which is 0. The arguments run source, source position, destination, destination position, length.

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

Post your comment

Please log in or register to post a comment.