Menu
Topics Index
...
`

Output Array2802


If you need explanation Read this topic

If you need Answer Take test on this topic

Output Array2802
What will be the output of the following program?
public class ArrayOutput
{
    public static void main(String s[])
    {
        int student_marks[] = new int[39];
        student_marks = new int[]{10, 20, 30};
        int[] my_array = null;
        my_array = student_marks;
        System.out.println("My Array length is: " + my_array.length);
        System.out.println("Student Marks Array length is: " + student_marks.length);
    }
}

A. My Array length is: 3
Student Marks Array length is: 39
B. My Array length is: 39
Student Marks Array length is: 39
C. My Array length is: 3
Student Marks Array length is: 3
D. Some other output
E. Compilation Error or Runtime Error
Topic: Java Array Initialization

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 have the Array concept..arrays can store the similar type of data elements in adjacent memory locations...arrays are fixed in size...arrays store homogeneous elements..
here we declare the students_marks array with the size 39..
here we re-assigning the student-marks to 10,20,30 so here student-marks size will become 3...
next we create another array reference my_array..and we assigned student_marks reference to it...so both references pointing same array now..so both lengths will be sam...3

Posted by Uday Kumar    2014-12-15 13:01:15


Answer is C....
Arrays are special objects in Java which can store homogenous elements in consecutive memory locations..And since it is an object,memory is allocated at run time..new int[39] will actually allocate the size required to store 39 int variables
In the next step , student_marks is reassigned using new int[]{10,20,30} which means size of the student_marks array is now reduced to three..
In the next step,my_array which is a new array reference  is created and is pointed to address referenced by student_marks array...
length is an attribute of an array which will print the size of the array...
Both the sizes of the arrays are printed which is three

Posted by Shashanka Mogaliraju    2014-12-15 17:59:27


ans c. in this program initially student-mark array is declared with 39 int element space allocation. aft that student- mark array assigned with 10,20,30.1st 3 index here 2d array type is used but any way it takes 10,20,30 as single dimension array.another array my- array initialized with null.this array reference pointing to nothing. nw studen- mark is assigned with my- array.both array has length 3.sop prints length of both array value.

Posted by Maheshwari Natarajan    2014-12-15 18:43:18


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Shashanka Mogaliraju,  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    2014-12-16 04:41:00


c

Posted by Sathish Govindaraj    2014-12-16 08:41:44


© meritcampus 2019

All Rights Reserved.

Open In App