LearnFrenzy
Sign in Take a free test

Arrays in Java · Predict the Output · Question 8

Lengths of a jagged array Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

What is the output of the following Java program?

Main.java
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links