Menu
Topics Index
...
`

Output Array


If you need explanation Read this topic

If you need Answer Take test on this topic

Output Array
What will be the output of the following program?
public class ArrayOutput
{
    public static void main(String s[])
    {
        int student_marks[] = new int[5];
        student_marks[2] = 25;
        System.out.println("Third Element = " + student_marks[2]);
        System.out.println("Fourth Element = " + student_marks[3]);
        student_marks[5] = 50;
        System.out.println("Fifth Element = " + student_marks[5]);
    }
}

A. Third Element = 25
Fourth Element = 0
Fifth Element = null
B. Third Element = 25
Fourth Element = 0
Fifth Element = 50
C. Third Element = 25
Fourth Element = 0
Fifth Element = 0
D. Third Element = 25
Fourth Element = 0
Fifth Element = {EMPTY}
E. Compilation Error or Runtime Error
Topic: Arraylist Access Using Index

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.

e

Posted by Kapil Sonii    2014-11-28 09:51:56


ans is E ,, students_marks[5] is not valid,, out of bound,,

Posted by Raju VarshnEy    2014-11-28 09:55:30


Ans is E...here we have the Arrays Concept...Array is an Collection of Similar data elements stored in Adjacent memory locations....and arrays are fixed in the size....we cant change the size of array once we created...

Arrays index starts from the 0.... if we create an array with size 10 than valid Indexes wil 0-9..
array size must be always postv number..if we give ngtv num than it will throw Exception....
we can access the elements in array by specifying the index...

Int a[]=new int[5];
All locations initilized with the 0's
a[0]=5;
Sop(a[0]);--5;

if we access the element from out of array size than Jvm will throw an Exception saying ArrayIndexOutOfBoundsException....

here we create the Array with size of 5 than valid indexes will be 0-4 in 2nd location we are putting the value 25 and printing so it will print 25
next we are printing the values at location it will print 0 Bcz v didnt Initilize it so it will print 0
and next we are putting  value at location 5 but here valid indexes is 0-4 so that it will throw Exception saying ArrayIndexOutOfBoundsException

Posted by Uday Kumar    2014-11-28 10:07:05


ans e. Runtime error-ArrayIndexOutOfBoundsException
Java supports the creation and manipulation of arrays, as a data structure. A bunch of Java data structures are implemented using arrays to store and expand their data. Thus, arrays are massively used in programming, as they provide a very fast and easy way to store and access pieces of data.
Each array consists of a concrete number of elements and thus, it has a fixed size. The index of an array is an integer value that resides in the interval [0, n-1], where n is the size of the matrix. If we request for an index that is either negative, or greater than or equal to the size of the array, an ArrayIndexOutOfBoundsException is thrown.
The ArrayIndexOutOfBoundsException is a RuntimeException thrown only at runtime. The Java Compiler does not check for this error during the compilation of a program. coming to this program initially array allocated with length of 5 element,array index position starts at 0,so 5 elements means we have index 0-4. here student_mark[2] index is assigned with 25 so this is 3rd element of the array. thn sop prints 2nd index value ,25 get printd. thn sop try to print 3rd index value 4th element of the array is not initialized with any number,so default int value 0 get printed.The elements of an array created in this way are
initialized to the default value for their type. The elements of an array of int are initialized to 0, for example, and
the elements of an array of objects are initialized to null. thn we are trying to assign 5 index of array to 50,which is 6th element of array,total size of array hve ly 5.but we are trying to access out of bound element,so this line throws ArrayIndexOutOfBound exception at time.

Posted by Maheshwari Natarajan    2014-11-28 18:48:27


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Ankur Verma. 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-11-29 02:10:41


© meritcampus 2019

All Rights Reserved.

Open In App