Menu
Question Index
...

Compare the following programs?

class ArrayOutput1
{
    public static void main(String s[])
    {
        int list[] = new int[] {1, 8, 27, 64, 125, 216, 343};
        int count = 1;
        int copy[][] = new int[list.length][count];

        for(int i = 0; i < count; i++)
        {
            for(int j = 0; j < list.length; j++)
            {
                copy[j][i] = list[j];
                System.out.print(copy[j][i] + "  ");
            }
        }
        System.out.println();
    }
}

class ArrayOutput2
{
    public static void main(String s[])
    {
        int list[] = new int[] {1, 8, 27, 64, 125, 216, 343};
        int count = 1;
        int copy[][] = new int[list.length][count];
        
        for(int i = 0; i < count; i++)
        {
            if(count < 10)
            {
                for(int j = 0; j < list.length; j++)
                {
                    copy[j][i] = list[j];
                    System.out.print(copy[j][i] + "  ");
                    count++;
                }
            }
        }
        System.out.println();
    }
}


Both ArrayOutput1 and ArrayOutput2
produce same output
ArrayOutput2 throws ArrayIndexOutOfBoundsException,
but ArrayOutput1 will compile and runs with out any exception.
ArrayOutput1 throws ArrayIndexOutOfBoundsException,
but ArrayOutput2 will compile and runs with out any exception.
Both ArrayOutput1 and ArrayOutput2 compile,
but they produce different output

Doubts

Problems

Topic: Learn Arrays And Loops

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App