Dynamic 2D Array Creation
Which of the following is an example of creating a 2D array with dimensions provided as arguments?
public class MatrixCreator {
public static int[][] createMatrix(int rows, int cols) {
return new int[rows][cols];
}
public static void main(String[] args) {
int[][] matrix = createMatrix(3, 4);
System.out.println(matrix.length + "x" + matrix[0].length);
}
}
A
public class MatrixCreator {
public static int[][] createMatrix(int rows, int cols) {
return new int[rows][cols];
}
public static void main(String[] args) {
int[][] matrix = createMatrix(3, 4);
System.out.println(matrix.length + "x" + matrix[0].length);
}
}
B
public class MatrixCreator {
public static int[][] createMatrix(int rows, int cols) {
return null;
}
public static void main(String[] args) {
int[][] matrix = createMatrix(3, 4);
System.out.println(matrix.length + "x" + matrix[0].length);
}
}
C
public class MatrixCreator {
public static int[][] createMatrix(int rows, int cols) {
return new int[rows-1][cols-1];
}
public static void main(String[] args) {
int[][] matrix = createMatrix(3, 4);
System.out.println(matrix.length + "x" + matrix[0].length);
}
}
D
public class MatrixCreator {
public static int[][] createMatrix(int rows, int cols) {
int[][] matrix = new int[rows][cols];
return matrix;
}
public static void main(String[] args) {
int[][] matrix = createMatrix(3, 4);
System.out.println(matrix.length + "x" + matrix[0].length);
}
}
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE