Java OCA OCP Practice Question 1658

Question

Which one of the following array declaration statements is not legal?.

Select the one correct answer.

  • (a) int []a[] = new int [4][4];
  • (b) int a[][] = new int [4][4];
  • (c) int a[][] = new int [][4];
  • (d) int []a[] = new int [4][];
  • (e) int [][]a = new int [4][4];


(c)

Note

The [] notation can be placed both after the type name and after the variable name in an array declaration.

Multidimensional arrays are created by constructing arrays that can contain references to other arrays.

The expression new int[4][] will create an array of length 4, which can contain references to arrays of int values.

The expression new int[4][4] will create the same two-dimensional array, but will in addition create four more one-dimensional arrays, each of length 4 and of the type int[].

References to each of these arrays are stored in the two-dimensional array.

The expression int[][4] will not work, because the arrays for the dimensions must be created from left to right.




PreviousNext

Related