Menu
Topics Index
...
`

Array Output761


If you need explanation Read this topic

If you need Answer Take test on this topic

Array Output761
What will be the output of the following program?
class ArrayOutput
{
    public static void main(String s[])
    {
        int a[] = {12, 15, 16, 17, 19};
        int I = 0;
        
        for(; I < 5 ;)
        {
            System.out.println(a[I]);
            I++;
        }
    }
}

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

Answer is a...for loop prints all the elements on the array initialised from index 0 to 4...whennl I is 5 condition fails and loop terminates

Posted by Shashanka Mogaliraju    2015-03-03 03:41:12


ans a. array is initialized with 5 elements. int I is initialized with 0.for loop is used to access   array elements. here in for loop we have I<5 condition ly.we can use this method of type also. here I is used which is already declared .thn sop prints array value a[0] thn I value get increased ,similarly this process happened for all the elements of array

Posted by Maheshwari Natarajan    2015-03-03 03:44:11


here option is A
in main function A array was created name A[]={12,15,16,17,19} with 5 elements and a integer vairable I
and its values was 0
and for loop is started with condition I<5 its(0 to 4)
for every iteration i it will print ith value in array A[]
0th iteration a[0]=12
1st iteration a[1]=15
2nd iteration a[2]=16

3rditeration a[3]=17
4th iteration a[4]=19
and prints(12,15,16,17,19) option is A

Posted by Goutham Singarapu    2015-03-03 03:48:33


answer a. array a is initialized with 5 elements. int I is initialized with 0.for loop is used to access array elements.for loop has ly condition.  here I is used which is already declared .thn sop prints array value a[0] =12,thn I is incremented ,I<5 condition passed sop prints a[1] 15,I incrmntd to 2,condition passed, sop prints a[2]=16,I incremented to 3,condition passed, sop prints a[3]=17,i increments to 4,condition passed, sop prints 19,aft this I is 5,condition failed so control goes out of loop.array has 5 elements so we can access these by using index0 to 4

Posted by NagaRaj Nataraj    2015-03-03 03:59:43


ANS IS A

--Here the concept of array is used.

--Array of 5 elements is declared.

--after that,for loop is used which runs till 5.it prints the elements of integer array one by one.
I.e. counter will have value from 0 to 4 as array is 0th indexed structure.

OUTPUT:
12 15 16 17 19

Posted by Mânïshå Mùlchåndânï    2015-03-03 14:28:24


This dose is now closed and the winners are Maheshwari Natarajan,  for 'First Correct Comment', Goutham Singarapu, NagaRaj Nataraj,  for 'Best Comment' and Mânïshå Mùlchåndânï 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-03-04 03:35:00


© meritcampus 2019

All Rights Reserved.

Open In App