1Z0-803 · Question #116
Which two are valid declarations of a two-dimensional array?
The correct answer is A. int [][] array2D; D. int[] array2D[];. Valid two-dimensional array declarations in Java specify the array dimensions using brackets adjacent to the type or adjacent to the variable name.
Question
Which two are valid declarations of a two-dimensional array?
Options
- Aint [][] array2D;
- Bint [2][2] array2D;
- Cint array2D[];
- Dint[] array2D[];
- Eint[][] array2D[];
How the community answered
(22 responses)- A91% (20)
- B5% (1)
- E5% (1)
Why each option
Valid two-dimensional array declarations in Java specify the array dimensions using brackets adjacent to the type or adjacent to the variable name.
A is a valid declaration where the [][] notation specifies a two-dimensional array type directly after the primitive type int. This declares array2D as a reference to a 2D integer array without initializing it.
B is incorrect because array sizes (e.g., [2][2]) cannot be specified during the declaration of the array reference; they are provided during initialization using new.
C is incorrect because int array2D[] declares a one-dimensional array of integers, not a two-dimensional one.
D is also a valid declaration where [] follows int to indicate an array of integers, and then [] follows array2D to indicate that array2D is an array of integer arrays, effectively a two-dimensional array. This is syntactically sugar for int[][] array2D;.
E is incorrect because int[][] array2D[] attempts to declare a three-dimensional array, as the final [] adds a third dimension to the int[][] type.
Concept tested: Java two-dimensional array declaration syntax
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Topics
Community Discussion
No community discussion yet for this question.