Menu
Topics Index
...
`

Array Output797


If you need explanation Read this topic

If you need Answer Take test on this topic

Array Output797
What will be the output of the following program?
class ArrayOutput
{
    public static void main(String s[])
    {
        int list[] = new int[] {1, 8, 7, 4, 5, 6, 3};
        int count = 1;
        int copy[][] = new int[list.length][list.length];
    
        for(int i = 0; i < list.length; i++)
        {
            for(int j = 0; j < list.length; j++)
            {
                copy[i][j] = list[i];
                System.out.print(copy[j][i] + "  ");
            }
            System.out.println();
        }
    }
}

A. 1 8 7 4 5 6 3
1 8 7 4 5 6 3
1 8 7 4 5 6 3
1 8 7 4 5 6 3
1 8 7 4 5 6 3
1 8 7 4 5 6 3
1 8 7 4 5 6 3
B. 1 1 1 1 1 1 1
0 8 8 8 8 8 8
0 0 7 7 7 7 7
0 0 0 4 4 4 4
0 0 0 0 5 5 5
0 0 0 0 0 6 6
0 0 0 0 0 0 3
C. 1 0 0 0 0 0 0
1 8 0 0 0 0 0
1 8 7 0 0 0 0
1 8 7 4 0 0 0
1 8 7 4 5 0 0
1 8 7 4 5 6 0
1 8 7 4 5 6 3
D. 1 1 1 1 1 1 1
8 8 8 8 8 8 8
7 7 7 7 7 7 7
4 4 4 4 4 4 4 
5 5 5 5 5 5 5
6 6 6 6 6 6 6
3 3 3 3 3 3 3
E. Throws ArrayIndexOutOfBoundsException
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 we create the 1D with the Some elements and again we create the 2D  array with the length of the 1D array which is 7..next we started the loops to travel through the 2D array..every time we are placing the I th element in the I th row...after assigning the element we are printing the elements reversly j th row and i th col

Posted by Uday Kumar    2015-03-04 03:41:18


Answer is C....consider for 1st iterations of outer and inner loops..
when i = 0 j=0 copy[0][0] = 1 but we are printing copy[0][0] = 1
when i= 0 j=1 copy[0][1] = 1 but we are printing copy[1][0] = 0 (since copy[1][0] is not yet initialised so default value is 0)
when i= 0 j=2 copy[0][2] = 1 but we are printing copy[2][0] = 0
---
---
At the end of 1st iteration of outer loop
1 0 0 0 0 0 0
Similarly
when i =1 j=0 copy[1][0] = 1 but we are printing copy[0][1] = 1
when i= 1 j=1 copy[1][1] = 8 but we are printing copy[1][1] = 8
when i= 1 j=2 copy[1][2] = 7 but we are printing copy[2][1] = 0 (since copy[2][1] is not yet initialised so default value is 0)
At the end of 2 nd iteration of outer loop
1 8 0 0 0 0 0

similarly for the rest

Posted by Shashanka Mogaliraju    2015-03-04 03:53:18


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Shashanka Mogaliraju,  for 'Best Comment' and Shashanka Mogaliraju for the 'Popular Comment'. The 'lucky liker' is Mânïshå Mùlchåndânï. 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-05 01:19:01


© meritcampus 2019

All Rights Reserved.

Open In App