Menu
Topics Index
...
`

Array Output757


If you need explanation Read this topic

If you need Answer Take test on this topic

Array Output757
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 = 0; i < 5; i += 2)
        {
            System.out.println(a[i]);
        }
    }
}

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

ans c.in this program array is initialized with 5 elements. for loop is used to access array element. here increment operation each time increase with 2, so indexes 0,2,4 get printed. so 12,16,19 get printed

Posted by Maheshwari Natarajan    2015-02-05 13:35:04


ANS IS C (12 16 19)

--Here Arrays are used.

--Array of integers is declared in program with length 5.

a[0]=12
a[1]=15
a[2]=16
a[3]=17
a[4]=19

--Here,Loop is declared wich runs from 0 to 5.After that counter 'i' is incremented with with 2. i.e.

i=0-->a[0]=12
i=2-->a[2]=16
i=4-->a[4]=19

--Therefore,12 16 19 will get printed.
OUTPUT:
12
16
19

Posted by Mânïshå Mùlchåndânï    2015-02-05 14:14:37


c

Posted by Shubham Bansal    2015-02-05 19:08:03


This dose is now closed and the winners are Maheshwari Natarajan,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment' and Shubham Bansal for the 'Popular Comment'. The 'lucky liker' is Mânïshå Mùlchåndânï. 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-06 03:27:34


© meritcampus 2019

All Rights Reserved.

Open In App