Menu
Question Index
...

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


Both LengthOfArray1 and LengthOfArray2 produce same output.
LengthOfArray1 and LengthOfArray2 produce different output.
LengthOfArray2 will compile and runs with out any error, but LengthOfArray1 will gives compilation error.
LengthOfArray1 will compile and runs with out any error, but LengthOfArray2 will gives compilation error.
Both LengthOfArray1 and LengthOfArray2 will fails to execute.

Doubts

Problems

Topic: Learn Arrays And Loops

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App