Which two are valid instantiations and initializations of a multi dimensional array?

Which two are valid instantiations and initializations of a multi dimensional array?

Which two are valid instantiations and initializations of a multi dimensional array?

A.
Option A

B.
Option B

C.
Option C

D.
Option D

E.
Option E



Leave a Reply 6

Your email address will not be published. Required fields are marked *


imyrta

imyrta

The right answer is:

A and D

B is wrong because you cannot specify an array dimension after an empty dimension
C is wrong because of type mismatch cannot convert from int to int[]
E is wrong because is a one-dimentional assignment two a two-dimentional array, same type mismathc

renko

renko

Answer: A,B

public class Main {

public static void main(String[] args) {
//int[][] array2D = {{ 0, 1, 2, 4}, {5, 6}}; //A-compiles: code is edited
int[][] array2D = new int [2][2]; //B-compiles
array2D[0][0] = 1;
array2D[0][1] = 2;
array2D[1][0] = 3;
array2D[1][1] = 4;
//int[][][] array3D = {{0, 1}, {2, 3}, {4, 5}}; //C
/*int [][][] array3D = new int [2][2][2]; //D
array3D[0][0] = array;
array3D[0][1] = array;
array3D[1][0] = array;
array3D[0][1] = array; */
//int[][] array2D = {0, 1}; //E
}

}
Output:
A: BUILD SUCCESSFUL (total time: 0 seconds)
B: BUILD SUCCESSFUL (total time: 0 seconds)
C: Exception in thread “main” java.lang.RuntimeException: Uncompilable source code – incompatible types: int cannot be converted to int[]
D: Exception in thread “main” java.lang.RuntimeException: Uncompilable source code – cannot find symbol symbol: variable array
E: Exception in thread “main” java.lang.RuntimeException: Uncompilable source code – incompatible types: int cannot be converted to int[]

renko

renko

D also compiles.

A- There is in the question no , between dimensions => int[][] array2Da = {{ 0, 1, 2, 4} {5, 6}}; //A-does not compiles