nerdexam
Oracle

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.

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

(22 responses)
  • A
    91% (20)
  • B
    5% (1)
  • E
    5% (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.

Aint [][] array2D;Correct

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.

Bint [2][2] array2D;

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.

Cint array2D[];

C is incorrect because int array2D[] declares a one-dimensional array of integers, not a two-dimensional one.

Dint[] array2D[];Correct

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;.

Eint[][] 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

#arrays#multi-dimensional arrays#array declaration syntax

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice