Menu
Topics Index
...
`

Array Output794


If you need explanation Read this topic

If you need Answer Take test on this topic

Array Output794
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++)
        {    
            copy[i][i] = list[i];
            
            for(int j = 0; j < list.length; j++)
            {
                System.out.print(copy[i][j] + "  ");
            }
            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 8 7 4 5 6 3
C. 1 0 0 0 0 0 0
0 8 0 0 0 0 0
0 0 7 0 0 0 0
0 0 0 4 0 0 0
0 0 0 0 5 0 0
0 0 0 0 0 6 0
0 0 0 0 0 0 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

Here,the concept of arrays is used.

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

After that,2D array is with name copy is declared.

--Nested FOR loop is written.inside outer loop element of list[I] is assigned to copy[I][I] I.e
For 1st iteration list[0] will be assigned to copy[0][0] I.e. 8 will be assigned to copy[0][0].

--This will be done for each loop iteration.only diagonal elements of copy array will be assigned values rest will be zero(0).

So,pattern shown in option C will be printed.

Posted by Mânïshå Mùlchåndânï    2015-03-10 08:06:35


This dose is now closed and the winners are Mânïshå Mùlchåndânï,  for 'First Correct Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is Sai Ram. 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-11 01:26:18


© meritcampus 2019

All Rights Reserved.

Open In App