Java OCA OCP Practice Question 3215

Question

Which code initializes the two-dimensional array matrix so that matrix[3][2] is a valid element?.

Select the two correct answers.

(a) int[][] matrix = {
        { 0, 0, 0 },//from w  ww. j  a va2s. co m
        { 0, 0, 0 }
    };
(b) int matrix[][] = new int[4][];
    for (int i = 0; i < matrix.length; i++) matrix[i] = new int[3];
(c) int matrix[][] = {
        0, 0, 0, 0,
        0, 0, 0, 0,
        0, 0, 0, 0,
        0, 0, 0, 0
    };
(d) int matrix[3][2];
(e) int[] matrix[] = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0} };


(b) and (e)

Note

For the expression matrix[3][2] to access a valid element of a two-dimensional array, the array must have at least four rows and the fourth row must have at least three elements.

Fragment (a) produces a 2 u 3 array.

Fragment (c) tries to initialize a two-dimensional array as a one-dimensional array.

Fragment (d) tries to specify array dimensions in the type of the array reference declaration.




PreviousNext

Related