Question 8
Back to Exercise
What is the output of the following Java program?
public class Main {
public static void main(String[] args) {
int[][] j = new int[3][];
j[0] = new int[]{1};
j[1] = new int[4];
j[2] = new int[]{5, 6};
int total = 0;
for (int[] row : j) total += row.length;
System.out.println(j.length + " " + j[1].length + " " + total + " " + j[1][3]);
}
}
- A 3 4 7 0 Correct
- B 3 4 7 null
- C 7 4 3 0
- D 3 3 7 0
Correct Answer: Option A
Each row is its own array. j[1] was created with new int[4], so its elements are 0.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!