Menu
Topics Index
...
`

Creating and initializing Array1405


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating and initializing Array1405
What will be the output of the following program?
public class DoubleStand
{
    public static void main(String avs[])
    {
        int[][] print = haiArray(4);
        for (int[] element : print)
        {
            for (int l = 0; l < element.length; l++)
            {
                System.out.print(l + " ");
            }
            System.out.println();
        }
    }
    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++;
            }
        }
        return printArray;
    }
}

A.
0 1 
0 1 2 
0 1 2 3
B.
1 2 
3 4 5 
6 7 8 9
C. 1
1 2
1 2 3
1 2 3 4
D.
2 3 
4 5 6 
7 8 9 10
E. Compilation Error or Runtime Error
Topic: Java Multidimensional Array

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.

answer is [ D ]  , Each element of the first dimension is actually another array with the length of the second dimension can be different in java.

Posted by Ashok Kumar Paritala    2014-02-27 08:48:44


ans:A...bcz array size is four...so value will incress based up on print values

Posted by Ashok Reddi    2014-02-27 08:58:47


Ans is A beacuse in haiarray method a 2d array will be created with row size 4 and printArray returns to print and for each loop iterates each row in print array and inner loop in for each loop will prints l values based on row length

Posted by Bharath Yelchuri    2014-02-27 08:59:48


A is the correct answer we are calling haiArray static method by passing 4 first it prints 0 and exits from the inner for loop and for the second iteration it prints 0 1 and then 0 1 2 and 0 1 2 3

Posted by Ram Reddy Bitla    2014-02-27 09:06:16


Congratulations Bharath Yelchuri. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-02-28 04:20:10


© meritcampus 2019

All Rights Reserved.

Open In App