info@meritcampus.com    +91-85006-22255
...
`

Three dimensional array
What will be the output of the following program?
class Fill3DArray
{
public static void main(String args[])
{
int[][][] M;
M = new int[2][2][3];
for (int row = 0; row < 1; row++)
for (int col = 0; col < 2; col++)
for (int ver = 0; ver < 3; ver++)
M[row][col][ver] = row + col + ver;
for (int row = 0; row < 1; row++)
for (int col = 0; col < 2; col++)
for (int ver = 0; ver < 3; ver++)
System.out.print(M[row][col][ver] + " ");
}
}

 A. 0 1 2 1 2 3 1 2 3 2 3 4 B. 0 1 2 1 2 3 C. 0 1 1 2 D. 0 1 2 E. Compilation Error or Runtime Error
Topic:

### User comments below.All of them might not be correct.

option B as
1st loop row=0,col=0,ver=0 to 2 so prints
0 1 2
later col values increments and row=0,col=1,ver=0 to 2 it prints
1 2 3
later col=2 and exits 2nd forloop
row value also increments and row=1
exits the outer for loop

Posted by Sasikanth Sharma    2014-07-21 03:35:15

0 1 2 1 2 3

Posted by Shubham Bansal    2014-07-21 03:44:10

when  row=0 and col=0 ver value will be incremented up to 2.
row=0;col=0;ver=0
m[0][0][0]=0+0+0;
row=0;col=0;ver=1
m[0][0][1]=0+0+1;
row=0;col=0;ver=2
m[0][0][2]=0+0+2;
the ans will be 0 1  2.
Then the value of col will be incremented to 1,
row=0;col=1;ver=0
m[0][1][0]=0+1+0;
row=0;col=1;ver=1
m[0][1][1]=0+1+1;
row=0;col=1;ver=2
m[0][1][2]=0+1+2;
so the ans will be 1 2 3
finally when col values will gets incremented the condition fails, so the value of row gets incremented there also condition fails. so our output will be printed as 0 1 2 1 2 3

Posted by Vino Krishnan    2014-07-21 05:37:57

B

Posted by Abhijit Bhusare    2014-07-21 13:04:01

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

Posted by Merit Campus    2014-07-22 04:26:23