1Z0-803 · Question #48
Which two are valid instantiations and initializations of a multi dimensional array?
The correct answer is A. Option A D. Option D. This question assesses the candidate's understanding of the correct syntax for declaring, instantiating, and initializing multi-dimensional arrays, including both rectangular and jagged array forms.
Question
Which two are valid instantiations and initializations of a multi dimensional array?
Exhibit
Options
- AOption A
- BOption B
- COption C
- DOption D
- EOption E
How the community answered
(25 responses)- A92% (23)
- B4% (1)
- C4% (1)
Why each option
This question assesses the candidate's understanding of the correct syntax for declaring, instantiating, and initializing multi-dimensional arrays, including both rectangular and jagged array forms.
This option represents a valid way to instantiate a multi-dimensional array (often called a jagged array). For example, `int[][] arr = new int[3][];` correctly declares a 2D array with 3 rows, where each row can later be assigned an array of variable length.
An invalid instantiation often involves specifying the second dimension without the first when using `new`, e.g., `int[][] arr = new int[][3];` which is syntactically incorrect.
An invalid declaration or instantiation could be incorrect placement of brackets like `int[3][4] arr;` or trying to combine dimension sizes with an initializer list in a way not supported by the language (e.g., `new int[2][3] { {1,2}, {3,4} }` in Java).
This option represents a valid way to instantiate a rectangular multi-dimensional array by specifying the size for all dimensions. For example, `int[][] arr = new int[2][3];` correctly creates a 2D array with 2 rows and 3 columns.
Invalid initializations can include assigning an initializer list directly to an inner array without `new int[]`, e.g., `arr[0] = {1,2};`, or incorrect syntax for creating anonymous arrays.
Concept tested: Multi-dimensional array instantiation and initialization syntax
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html#multidimensional
Topics
Community Discussion
No community discussion yet for this question.
