Topics Index

Array Output759

If you need explanation Read this topic

If you need Answer Take test on this topic

Array Output759
What will be the output of the following program?
class ArrayOutput
    public static void main(string s[])
        int a[] = {12, 15, 16, 17, 19};

        for(int i = 5; i > 0; i--)

A. 19
B. 17
C. 19
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.


Posted by Shubham Bansal    2015-02-09 15:40:08

here option  E:
beacuse array a[] consist of 5 elements it indexes are from
but in for(int i=5;i>0;i--) in  loop were a variable i initialized to 5
and it was greater than 0(i>0) it ok but in
in we accesing a[i] means=> a[5] it gives arrayOutOfBounds expection
beacuse array a[] consist only 5 elements(index[0 to 4] but were accesing a[5](6th element) that it gives exception arrayOutOfBound
option is E

Posted by Goutham Singarapu    2015-02-09 16:07:10


Posted by Harish Kumar G    2015-02-09 16:37:04


--Here,concept of array is used.

--Array of integer is declared and initialized with the values

--After that,"FOR LOOP" is used where it starts from 5 to 0 by decrementing the counter value.Inside the loop element at position i is printed.

i=2 and i>0(true)
Control goes inside the loop and prints a[5].But array is of size 5 from index 0 to 4.Therefore,ArrayIndrexOutOfBounds will be thrown.

--Therefore,Runtime Error is there in program.

Posted by Mânïshå Mùlchåndânï    2015-02-09 17:22:11

ans e. run time exception ,ArrayIndexOutOfBound get thrown .in this program we have sigle dimension array which is initialized with 5 elements. by default index values taken from 0, so here we have indexes 0,1,2,3,4.a[0]=12,a[1]=15,a[2]=16,a[3]=17,a[4 ]=19.for loop is used to access array element.     here i is initialized with 5,i> 0 condition passed, in sop we tried to print a[5] which is not in present boundary, we don't have 5 th indexed allocated values,this is an illegal access. we are here trying to access out of boundary value,so this line throws ArrauIndexOutOfBoundary program abruptly stopped bcz of this run time exception

Posted by Maheshwari Natarajan    2015-02-09 19:07:53

Ans is we have an int array initialized with the 5 int elements so these 5 elements stores in an adjcent memory locations..and array index starts from 0-4 in the for loop we are trying to access the element at 5th location which is illegal so here JVM will throw an Exception ArrayIndexOutOfBoundsException

Posted by Uday Kumar    2015-02-10 03:43:18

This dose is now closed and the winners are Goutham Singarapu,  for 'First Correct Comment', Maheshwari Natarajan, Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Gopinath Manchikanti. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-02-10 04:21:29

© meritcampus 2019

All Rights Reserved.

Open In App