Menu
Topics Index
...
`

Arrays1379


If you need explanation Read this topic

If you need Answer Take test on this topic

Arrays1379
What will be the output of the following program?
public class LengthOfArray {
    public static void main(String[] args) {
        int[][][] x = new int[3 * 1][][];
        int i, j;
        int size = 0;
        x[0] = new int[3 * 2][];
        x[1] = new int[1 * 2][];
        x[2] = new int[2 * 2][];
        for (i = 0; i < x.length; i++) {
            for (j = 0; j < x[i].length; j++) {
                x[i][j] = new int[i + j + 1];
                size += x[i][j].length;
            }
        }
        System.out.println("Size = " + size);
    }
}

A. Size = 6
B. Size = 12
C. Size = 38
D. Size = 44
E. Compilation Error or Runtime Error
Topic: Learn Arrays And Loops

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is D... as length variable of array always returns the size of base array.. so x.length=3,x[0].length=6,x[1].length=2,x[2].length=4..by calculating we will get size=44

Posted by Krishna Sai    2013-06-14 07:27:12


Ans is d....x is a jagged array which stores group of arrays within it....
X[0] has 6 rows in which each column hold arrays of size 1 2 3 4 5 6(6×7/2)
X[1] has 2 rows each holds 1 2 size arrays( 2*3/2 )

X[2] has 4 rows each holds 1 2 3 4 size arrays (4×5/2)

Thus total length of size becomes 44

Posted by Prudhvi Raj    2013-06-14 08:23:33


Congratulations Krishna Sai Mulpuri. You are this dose winner. Please send us your mobile details and we will process the recharge asap.

Posted by Merit Campus    2013-06-14 12:28:43


Received rc !! thanks :)

Posted by Krishna Sai    2013-06-17 08:50:43


© meritcampus 2019

All Rights Reserved.

Open In App