Menu
Topics Index
...
`

Array Output793


If you need explanation Read this topic

If you need Answer Take test on this topic

Array Output793
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[i][j] + "  ");
            }
            System.out.println();
        }
    }
}

A. 1 8 7 4 5 6 3
B. 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
C. 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
D. Compilation Error
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.
In this program we are copying the nth element of list array to the all elements of nth row of the copy array..

Execution :
We are taking number of rows and columns as the length of the list array..
And we are copying the 'i' th element of the list array to the 'i'th row and 'j'th column of the copy array and we are printing the element..

Posted by Gopinath Manchikanti    2015-03-09 04:05:02


ANS is C

--Here,the concept of array (1D and 2D) is used.

--First of all,list array is declared with the  following 7 elements:
1 8 7 4 5 4 3

--After ttha,copy 2D array is declared with size equal to size of list array.

--Then,FOR loop is used in which outer loop as well inner loop runs till the length of list array.

Inside inner for loop,0th element of list is assigned to all the elements of 0th row of copy array.

I.e
Row 0 of copy will contain following:
1 1 1 1 1 1 1

OUTPUT:
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.

Posted by Mânïshå Mùlchåndânï    2015-03-09 05:46:26


This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Gopinath Manchikanti, Mânïshå Mùlchåndânï,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Srikar Mogaliraju. 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-10 03:45:28


© meritcampus 2019

All Rights Reserved.

Open In App