Menu
Topics Index
...
`

Array Output766


If you need explanation Read this topic

If you need Answer Take test on this topic

Array Output766
Compare the following programs?
class ArrayOutput1
{
    public static void main(String s[])
    {
        int marks[] = new int[4];
        marks = new int[] {87, 76, 84, 57};
        
        for(int i = 0; i < 4; i++)
        {
            System.out.println(marks[i]);
        }
    
    }
}

class ArrayOutput2
{
    public static void main(String s[])
    {
        int marks[] = new int[4];
        marks = new int[{87, 76, 84, 57}];
        
        for(int i = 0; i < 4; i++)
        {
            System.out.println(marks[i]);
        }
    
    }
}

A. Both ArrayOutput1 and ArrayOutput2 produce same output
B. ArrayOutput2 does not compile, but ArrayOutput1 will compile
C. ArrayOutput1 does not compile, but ArrayOutput2 will compile
D. Both ArrayOutput1 and ArrayOutput2 compile, but they produce different output
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 B..here we have the Arrays concept where arrays can store the similar kind of the elements in the Adjacent memory locations..and arrays are fixed in the size.and arrays can be declared in many ways..

Case 1:
her we create the array of int with the size 4 and next for that reference we assigned the other int array which has the 4 elements  next we are printing using the for loop so it will print elements

Case 2:her we create the array of int with the size 4 next again we are trying to assign the another array which is specified in the [] in array we can not declare like this here compiler will raise an error

Posted by Uday Kumar    2015-02-16 11:14:24


C

Posted by Aditya Aggarwal    2015-02-16 18:53:34


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar 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-02-17 04:01:47


© meritcampus 2019

All Rights Reserved.

Open In App