Menu
Topics Index
...
`

Arrays1380


If you need explanation Read this topic

If you need Answer Take test on this topic

Arrays1380
What will be the output of the following program?
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[i + j + 1];
                k++;
            }
        }
        System.out.println("K value is: " + k);
    }
}

A. K value is: 7
B. K value is: 9
C. K value is: 10
D. K value is: 11
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 C.
Here in the program we are taking a 3D array in which every dimension will be having 3 2D arrays.. and the first 2D array of length 3 rows and second 2D array of length 2 rows and third 2D array of length 5 rows...

Then we are taking a variable 'k' as counter.. and we using nested for loops to create every possible 1D array and incrementing the 'k' value...

As the outer for loop will iterate through length of 3D array i.e., 3 times(from 0 to 2) and inner for loop will iterate through 2D array accordingly i.e., when i = 0, j = 0 to 2, for i = 1, j = 0 to 1, for i = 2, j = 0 to 4... so as per the program k will be incremented 10 times...

Thus the output will be printed as :
K value is : 10

Posted by Gopinath Manchikanti    2015-02-27 14:38:30


ANS IS C

AS inside x[0] there are 3 rows so inner j loop runs 3 times and similaraly for x[1] and x[2] there are 5 and 2 rows so k=7

k value is:7

Posted by Mânïshå Mùlchåndânï    2015-02-27 18:33:41


This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Gopinath Manchikanti,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is Sonu Nagdev. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-03-02 06:59:26


© meritcampus 2019

All Rights Reserved.

Open In App