Menu
Topics Index
...
`

Creating two dimensional Array


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating two dimensional Array
What will be the output of the following program?
public class Stand {
    public static void main(String avs[]) {
        haiArray(4);
    }
    static int[][] haiArray(int size) {
        int[][] printArray = new int[size][];
        int val = 1;
        for (int i = 0; i < printArray.length; i++) {
            printArray[i] = new int[i + 1];
            for (int j = 0; j < printArray[i].length; j++) {
                printArray[i][j] = val++;
                System.out.print(printArray[i][j] + " ");
            }
            System.out.println();
        }
        return printArray;
    }
}

A. 1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
B. 1 2 3 4
5 6 7
8 9
10
C.
2 3 
4 5 6 
7 8 9 10
D. 1 2 3 4
E. Compilation Error or Runtime Error
Topic: Static and Non Static Variables - Static and Non Static Methods

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 c : because for first time size of printArray is 1 and next it becomes 2 and next 3 and next 4...and hence that output

Posted by Haritha Damarla    2013-06-28 03:45:52


and the val variable is post incrementing here...so first initial valuue of val=1 is prnted and then incremented..

Posted by Haritha Damarla    2013-06-28 03:48:23


first for loop iterates for 4 times ( for i=0,1,2,3) and 2nd for loop iterates for (j=0;j=0,1;j=0,1,2;j=0,1,2,3)

Posted by Haritha Damarla    2013-06-28 03:55:54


ans is C bcoz the inner for loop iterates for i+1 tyms... and their is post increment while assigning the value to prinatarray[i][j] ...so printarray[0] has 1 and printarray[1] has 2 , 3 and so on...

Posted by Mahesh Kumar    2013-06-28 04:55:51


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

Posted by Merit Campus    2013-06-28 08:39:49


Thanq.. i got Rc

Posted by Mahesh Kumar    2013-07-02 08:22:06


© meritcampus 2019

All Rights Reserved.

Open In App