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 1 of 2.

Sri said:   1 decade ago
As for the above declaration :

when the outer loop start to execute starts from 0, 1, 2 is the size of the 'x' 3 is size of x. And every x initializes inner loop with their sizes so, when outer loop starts execution x takes first argument at that time inner loop executes 4 times, after that jvm come out of inner loop and increment apply for outer loop at that time 'x' takes 1 and it will be executes 2 times...and same as 3 rd time.

As for coding
x[0] is i=0 it comes inner loop j=0.

and it returns[0+0+1].

i=0,j=1 it gives [0+1+1].
i=0,j=2 it gives [0+2+1].
i=0,j=3 it gives [0+3+1].

And the size of x[0] is only 4 .

So,loop comes inner loop again it increment.

i=1,j=0 it gives [1+0+1].
i=1,j=1 it gives [1+1+1].

And the size of x[1]is 2.

So,loop comes inner loop again it increment

i=2,j=0 it gives [2+0+1].
.
.
.
i=2,j=4 it gives [2+4+1]

Totally it executes 11 lines.

Please try to understand it.
(1)

Gopal said:   1 decade ago
When i=0 then x[0].length=4 (given in the program that x[0] = new int[4][]).
Here j loop repeats 4 times.

When i=1 then x[1].length=2 (given in the program that x[1] = new int[2][]).
Here j loop repeats 2 times.

When i=2 then x[2].length=5 (given in the program that x[2] = new int[5][]).
Here j loop repeats 5 times.

Totally it executes 11 lines.
(1)

Chidu said:   9 years ago
0th iteration->0 1 2 3;
1st iteration->0 1;
2ns iteration->0 1 2 3 4.
count total number in the right side,
So ans is 11.
(1)

HARSHAD said:   9 years ago
x[0] = new int[4][];
x[1] = new int[2][];
x[2] = new int[5][];
4+2+5=11.
(1)

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

Srrivatsan Sekar said:   5 years ago
Yes, @abhijit.

Java allows 3D arrays. Because in java we have 1D and multi D arrays. In the case of multi-D arrays, we can have 2D and 3D arrays.

Tanu said:   7 years ago
I guess the answer should be 9. It could have been 11 if the condition for the loops would be i/j<=x. Length().

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

Logaa said:   9 years ago
Can anyone please explain that nested for loop operation in detail? please.

Rahul said:   10 years ago
Well explained @Sri.

@All.
Please check Sri's explanation to understand the answer.


Post your comments here:

Your comments will be displayed after verification.