Menu
Topics Index
...
`

Array Output760


If you need explanation Read this topic

If you need Answer Take test on this topic

Array Output760
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 - 1; i > 0; i--)
        {
            System.out.println(a[i]);
        }
    }
}

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

Ans is C.
here in the program we are printing the values of array in reverse order by considering the last index of array to the starting index of array.. but as the condition resembles that i should be greater than 0.. so the first element of the array will not be printed... so the program will print elements from index 4 to 1... Output is
19
17
16
15

Posted by Gopinath Manchikanti    2015-02-25 03:54:07


here option C
excution starts from main() and a array was created  name 'a' with 5 elements
a[0]=12
a[1]=15
a[2]=16
a[3]=17
a[4]=19
and a for loop with a variable i was created with value i=5-1 means i=4
for(i=4;i>0;i--)
{print(a[i])}
  i value=4 it is greater than 0 and prints a[4]=> 19  and i value decremented by 1 and i=3

  i value=3 it is greater than 0 and prints a[3]=> 17  and i value decremented by 1 and i=2

  i value=2 it is greater than 0 and prints a[2]=> 16  and i value decremented by 1 and i=1

  i value=1 it is greater than 0 and prints a[1]=> 15  and i value decremented by 1 and i=0

  i value=0 it is not greater than 0 and condition fails and it comeout from loop.
result prints

19
17
16
15 option C

Posted by Goutham Singarapu    2015-02-25 04:01:19


Ans is C..here we have the Arrays concept..arrays stores the similar kind of elements in the adjacent memory locations and indexes starts from the 0 to till the size-1, if we try to access the element out of the index range than we vl get the Exception and arrays are fixed in the size...
Execution:
here we create the int array with the 5 elements so indexes of the array is 0-4..next we starts an loop and loop control initialized with the 4 and iterating till the i is greater than 0 it will iterate till i=1 so it will print the elements from index 4 to index 1 so
19
17
16
15

Posted by Uday Kumar    2015-02-25 15:36:00


Answer is c

Posted by Shashanka Mogaliraju    2015-02-25 18:18:31


C

Posted by Muhammad Shoaib Murtaza    2015-02-26 01:39:42


This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Goutham Singarapu,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Shashanka Mogaliraju. 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-26 02:33:29


© meritcampus 2019

All Rights Reserved.

Open In App