Dynamic 2D Array Creation
Which of the following code segments correctly defines and utilizes a method to create a two-dimensional int array with dimensions specified by its arguments?
A
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);
}
}
B
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);
}
}
C
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);
}
}
D
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);
}
}
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | bommasam000 | 1 | 4 | 1m 44s | -34 |
| #2 | ajcantuwilson0325 | 0 | 1 | 2m 00s | -130 |
| #3 | varunrajaram1 | 0 | 1 | 4m 37s | -287 |
| #4 | psak12 | 0 | 1 | 16m 32s | -1,002 |
Items per page:
10
1 – 4 of 4
APFIVE