Sunday, August 15, 2010

Array: CopyArray.java

Courtesy of Sun Web Learning Center

1. class CopyArray {
2.   public static void main(String [] args) {
3.     int[] x = {1, 2 ,3};
4.     // insert code here
5.   }
6. }


Which two, inserted independently at line 4, will compile? (Choose two.)

A     int[] y1 = x;
B     int[] y2; y2 = x;
C     int[] y3 = x.copy();
D     int[] y4; for(int z : x) { y4[z] = x[z]; }

A and B are correct. Option C is incorrect because arrays do not have a copy method. Option D is incorrect because y4 was NOT initialized.

No comments:

Post a Comment