nerdexam
Oracle

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…

Creating and Using Arrays

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

(48 responses)
  • A
    92% (44)
  • B
    4% (2)
  • C
    2% (1)
  • E
    2% (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

#2D arrays#Array syntax#Multidimensional arrays#Declarations

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice