Java | Arrays | Question 5

filter_none

edit
close

play_arrow

link
brightness_4
code

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();
        }
    }

chevron_right


(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



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.