1Z0-808 · Question #99
Which two are valid declarations of a two-dimensional array?
The correct answer is A. int [][] array2D; D. int[] array2D[]. In Java, the bracket notation [] can be placed after the type, after the variable name, or split between both. Option A (int [][] array2D) uses the conventional style with both bracket pairs after the type, while option D (int[] array2D[]) splits them - one pair after int and…
Question
Options
- Aint [][] array2D;
- Bint [2][2] array2D;
- Cint array2D[][];
- Dint[] array2D[];
- Eint[][] array2D[];
How the community answered
(48 responses)- A92% (44)
- B4% (2)
- C2% (1)
- E2% (1)
Explanation
In Java, the bracket notation [] can be placed after the type, after the variable name, or split between both. Option A (int [][] array2D) uses the conventional style with both bracket pairs after the type, while option D (int[] array2D[]) splits them - one pair after int and one after the variable name - which Java's spec explicitly permits, making both unambiguously valid 2D array declarations.
B is wrong because you cannot specify array sizes inside the brackets at declaration time (int [2][2] array2D is illegal); dimensions belong in the instantiation (new int[2][2]), not the declaration. E is wrong because placing brackets after both int[][] and the variable name (int[][] array2D[]) results in a 3D array (int[][][]), not a 2D one. C (int array2D[][]) is actually valid Java syntax and is commonly cited as correct in other exam versions - its inclusion as a distractor here may reflect a flaw in this specific question's answer key.
Memory tip: Think of the brackets as "attaching" to the variable from either side - left (on the type) or right (on the name), or both - as long as the total bracket pairs equal the number of dimensions. Watch out for size values inside declaration brackets (illegal) and extra bracket pairs that silently add a dimension.
Topics
Community Discussion
No community discussion yet for this question.