Java OCA OCP Practice Question 2409

Question

Which of the following are valid lines of code to define a multidimensional int array?

  • a int[][] array1 = {{1, 2, 3}, {}, {1, 2,3, 4, 5}};
  • b int[][] array2 = new array() {{1, 2, 3}, {}, {1, 2,3, 4, 5}};
  • c int[][] array3 = {1, 2, 3}, {0}, {1, 2,3, 4, 5};
  • d int[][] array4 = new int[2][];


a, d

Note

Option (b) is incorrect.

This line of code won't compile because new array() isn't valid code.

Unlike objects of other classes, an array isn't initialized using the keyword new followed by the word array.

When the keyword new is used to initialize an array, it's followed by the type of the array, not the word array.

Option (c) is incorrect.

To initialize a two-dimensional array, all of these values must be enclosed within another pair of curly braces, as shown in the code in option (a).




PreviousNext

Related