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.

Harsha said:   1 decade ago
What is the length of the array?

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

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

Maheshthakuri said:   1 decade ago
@shwetha

I'm not sure if I'm correct or not but i think the following program work as follow.

OUTER LOOP will execute 3 times as x.length will give 3 as array length.

INNER LOOP will execute as below
1.When x = 0(x from outer loop)
then inner loop will execute 4 times as size of array is 4

2.When x =1(x from outer loop)
then inner loop will execute 2 times as size of array is 2

3.When x = 2(x from outer loop)
then inner loop will execute 5 times as size of array is 5

Now lets see the proper execution

1st Iteration outer loop
i=0
1st iteration inner loop
i=0
j=0
so x[][] = will have 1 element i.e 1 new int[0+0+1]
output: size 1

2nd iteration inner loop
i=0
j=1
so x[][] = will have 2 element. One at x[0][0] i.e 1
and second at x[0][1] i.e 2 as new int[0+1+1].
output: size 2

3rd iteration inner loop
i=0
j=2
so x[][] = will have 3 element.
One at x[0][0] i.e 1,second at x[0][1] i.e 2
and third at x[0][2] i.e 3 as new int[0+2+1]
output: size 3

4th iteration inner loop
i=0
j=3
so x[][] = will have 4 element.
One at x[0][0] i.e 1,second at x[0][1] i.e 2,
third at x[0][2] i.e 3 and 4th at x[0][3] i.e 4 as
new int[0+3+1]
output: size 4

5th Iteration
Condition become false as j = 4(we need j<x[i].length)
and as told earlier length of x[0] is 4(mentioned above)

similarly we do for x[1] and x[2] to get the other output.
x[3] will case the condition to become false and break out of the Outer loop also.


This is how i think the program is working. If any error, correction or doubt you can post here.

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.

Gopal said:   10 years 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)

Satyam mishra said:   9 years ago
I didn't get. Please tell me x is three dimensional array, without complete initialization how we insert an element in the array.

Please any one explain in brief.

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

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

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

Well said, Thanks for explaining it.


Post your comments here:

Your comments will be displayed after verification.