Menu
Topics Index
...
`

Arrays1381


If you need explanation Read this topic

If you need Answer Take test on this topic

Arrays1381
Compare the following programs?
public class LengthOfArray1 {
    public static void main(String[] args) {
        int[][][] x = new int[3][][];
        int i, j;
        x[0] = new int[3][];
        x[1] = new int[2][];
        x[2] = new int[5][];
        int k = 0;
        for (i = 0; i < x.length; i++) {
            for (j = 0; j < x[i].length; j++) {
                x[i][j] = new int[1];
                k++;
            }
        }
        System.out.println("K value is: " + k);
    }
}
public class LengthOfArray2 {
    public static void main(String[] args) {
        int[][][] x = new int[3][][];
        int i, j;
        x[0] = new int[3][];
        x[1] = new int[2][];
        x[2] = new int[5][];
        int k = 0;
        for (i = 0; i < x.length; i++) {
            for (j = 0; j < x[i].length; j++) {
                x[i][j] = new int[i + j + 55];
                k++;
            }
        }
        System.out.println("K value is: " + k);
    }
}

A. Both LengthOfArray1 and LengthOfArray2 produce same output.
B. LengthOfArray1 and LengthOfArray2 produce different output.
C. LengthOfArray2 will compile and runs with out any error, but LengthOfArray1 will gives compilation error.
D. LengthOfArray1 will compile and runs with out any error, but LengthOfArray2 will gives compilation error.
E. Both LengthOfArray1 and LengthOfArray2 will fails to execute.
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 a: both of them produce d same output as in both of them for loops are d same and declarations are same

Posted by Krishna Murthy    2013-06-26 12:45:58


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

Posted by Merit Campus    2013-06-27 03:25:07


© meritcampus 2019

All Rights Reserved.

Open In App