Java Programming - Language Fundamentals - Discussion

Discussion Forum : Language Fundamentals - Finding the output (Q.No. 7)
7.
In the given program, how many lines of output will be produced?
public class Test 
{
    public static void main(String [] args) 
    {
    int [] [] [] x = new int [3] [] [];
    int i, j;
    x[0] = new int[4][];
    x[1] = new int[2][];
    x[2] = new int[5][];
    for (i = 0; i < x.length; i++)
    {
        for (j = 0; j < x[i].length; j++) 
        {
            x[i][j] = new int [i + j + 1];
            System.out.println("size = " + x[i][j].length);
        }
    }
    }
}
7
9
11
13
Compilation fails
Answer: Option
Explanation:

The loops use the array sizes (length).

It produces 11 lines of output as given below.

D:\Java>javac Test.java

D:\Java>java Test
size = 1
size = 2
size = 3
size = 4
size = 2
size = 3
size = 3
size = 4
size = 5
size = 6
size = 7

Therefore, 11 is the answer.

Discussion:
17 comments Page 2 of 2.

Komal said:   9 years ago
I didn't get it. Someone explain me to understand the concept.

MSH said:   9 years ago
@Maheshthakuri and @Sri.

Well said, Thanks for explaining it.

Shwetha said:   1 decade ago
I didn't get this code please explain in detail.

Hye Jung said:   9 years ago
Why is the second for loop executed first?

Vicky said:   8 years ago
How the size of array becomes 4?

Harsha said:   2 decades ago
What is the length of the array?

Abhijit said:   1 decade ago
Is 3-D array allowed in java?


Post your comments here:

Your comments will be displayed after verification.