1Z0-803 · Question #261
Given the code fragment: // insert code here arr[0] = new int[3]; arr[0][0] = 1; arr[0][1] = 2; arr[0][2] = 3; arr[1] = new int[4]; arr[1][0] = 10; arr[1][1] = 20; arr[1][2] = 30; arr[1][3] = 40; Whic
The correct answer is C. int [] [] arr=new int [2] [ ]; E. int [] [] arr=new int [2] [0];. To correctly declare and initialize a two-dimensional jagged array in Java, you must specify the size of the first dimension when creating the array object. Options C and E provide valid syntax for declaring the outer array, allowing subsequent assignments to its elements to comp
Question
Given the code fragment:
// insert code here arr[0] = new int[3]; arr[0][0] = 1; arr[0][1] = 2; arr[0][2] = 3; arr[1] = new int[4]; arr[1][0] = 10; arr[1][1] = 20; arr[1][2] = 30; arr[1][3] = 40; Which two statements, when inserted independently at line // insert code here, enable the code to compile?
Options
- Aint [] [] arr = null;
- Bint [] [] arr=new int [2];
- Cint [] [] arr=new int [2] [ ];
- Dint [] [] arr = new int [] [4];
- Eint [] [] arr=new int [2] [0];
- Fint [] [] arr=new int [0] [4];
How the community answered
(23 responses)- A4% (1)
- C78% (18)
- D4% (1)
- F13% (3)
Why each option
To correctly declare and initialize a two-dimensional jagged array in Java, you must specify the size of the first dimension when creating the array object. Options C and E provide valid syntax for declaring the outer array, allowing subsequent assignments to its elements to compile successfully.
Declaring `arr` as `null` means the array itself is not initialized, and attempting to access `arr[0]` would result in a `NullPointerException` at runtime.
The syntax `new int[2]` creates a one-dimensional array of integers, which cannot be assigned to a two-dimensional array variable `int[][] arr` due to a type mismatch.
The statement `int [][] arr = new int [2][];` correctly declares a two-dimensional array variable `arr` and initializes its first dimension to hold two array references, leaving the inner arrays uninitialized (null) which are then assigned later. This is the standard way to declare a jagged array where inner array sizes are determined later.
The syntax `new int[][4]` is invalid for array creation; you cannot specify the second dimension's size without specifying the first dimension's size.
The statement `int [][] arr = new int [2][0];` declares a two-dimensional array `arr` with an outer dimension of size 2, where each inner array is initially an empty array of integers (length 0). Although these empty inner arrays are immediately reassigned by the subsequent code, this declaration is syntactically valid and allows the program to compile.
Declaring `int [][] arr = new int [0][4];` creates an outer array of length zero, which means `arr[0]` would immediately result in an `ArrayIndexOutOfBoundsException` at runtime when trying to assign to its first element.
Concept tested: Java Jagged Array Initialization
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Topics
Community Discussion
No community discussion yet for this question.