Skip to content
Related Articles

Related Articles

Java | Arrays | Question 5
  • Difficulty Level : Easy
  • Last Updated : 16 Jan, 2014




public class Main {
    public static void main(String args[]) {
        int arr[][] = new int[4][];
        arr[0] = new int[1];
        arr[1] = new int[2];
        arr[2] = new int[3];
        arr[3] = new int[4];
   
        int i, j, k = 0;
        for (i = 0; i < 4; i++) {
            for (j = 0; j < i + 1; j++) {
                arr[i][j] = k;
                k++;
            }
        }
        for (i = 0; i < 4; i++) {
            for (j = 0; j < i + 1; j++) {
                System.out.print(" " + arr[i][j]);
                k++;
            }
            System.out.println();
        }
    }

(A) Compiler Error
(B)

 0
 1 2
 3 4 5
 6 7 8 9 

(C)

 0
 0 0
 0 0 0
 0 0 0 0 

(D)

 9
 7 8
 4 5 6
 0 1 2 3 


Answer: (B)

Explanation: In Java, we can create jagged arrays.  Refer Jagged Array in Java for details.

Quiz of this Question

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :