Topics Index

Array Accessing753

If you need explanation Read this topic

If you need Answer Take test on this topic

Array Accessing753
What will be the output of the following program?
class ArrayOutput
    public static void main(String s[])
        int a[] = new int[50];

        int i = 27 % 11;
        int j = 5 * 3;
        int k = j - i;

        a[i] = i;
        a[j] = j;
        a[k] = k;

        int sum = 0;

        for(int l = 0; l < a.length; l++)
            sum += a[l];

        System.out.println("Sum = " + sum);

A. Sum = 40
B. Sum = 30
C. Sum = 10
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 we create an integer array with the size of 50..and here we have the 3 local variables i,j,k here i will assigned with the 5 and j is assigned with the 15 and k assigned with the 10 and we are inserting these elements in the array in same positions and remaining elements will be 0s..using for loop we are adding all these elements 15+5+10+0+0etc=30

Posted by Uday Kumar    2015-01-12 08:46:18

Ans is Option C... While executing start variable a is an array of an integer with size 50 and the variable i value will initialize with 5 and the variable j will be 15 and the variable k will be 10 and a[i] will become a[5] and this location will be initialized with the value 5 and a[j] will be a[15] and this value will be 15 and a[k] will become a[10] and this location will be initialized with the value 10 and variable sum will be initialized with the value 0 and compiler will enter into loop with the range from 0 to 50 because a.length value is 50.... other than a[5],a[10],a[15] remaining all locations of variable a[--] values are 0. so while executing the statement inside the loop i.e sum+=a[l]  will execute 50 times  and the values a[5],a[10] and a[15]  having values with 5,10 and 15 respectively so sum became sum=5+10+15=30 so option c is correct sum=30

Posted by Bhagi Bhagyasri    2015-01-12 09:32:48

i'll go with option B .. as the execution starts from main() method .. every thing goes smooth  till "sum =0" ( at this point i = 5 ,j=15,k=10 and array values are like a[5]=5 , a[15]=15 , a[10]=10) we need to analyse the for loop as  "l" varies from 0 to till 49 .. and what happens inside loop is like addition of the sum variable continuously with the array values so ... at 6th iteration (a[5]=5) sum+=a[5] gives 5 and then at 11th  iteration (a[10]=10) sum+=a[10]  gives 15 .. and then at 16th iteration (a[15]=15) sum+=a[15]  gives 30 ...then after that all the array values are 0 by default... SO final answer will be "sum=30"..

Posted by ?????????? ?????    2015-01-12 09:56:16


Posted by Gaurav Bansal    2015-01-13 03:37:20

ANS IS A (sum=30)

--Here Basic addition of elements of array is done.

--In above example Array of integer is declared with the size "50".Whenever array of elements is declared,all the elements are declared with the default value of that data type of which array is declared.
As here Integer data type i.e. int is,all the elements will be initialized with default value zero(0).

--The following chart summarizes the default values for the above data types.
Data Type Default Value (for fields)
byte 0
short 0
int 0
long 0L
float 0.0f
double 0.0d
char '\u0000'
String (or any object)  null
boolean false

So,Here 50 integer elements will be initialized with value 0.

--After that i=27%11 ---> i=5
j=5*3 ---> j=15
k=j-i=15-5 ----> k=10

--Then these values are assigned to locations numbers same as values.

i.e. a[i]=i --->a[5]=5
     a[i]=i --->a[15]=15
     a[i]=i --->a[10]=10

--Sum is initialized to 0.For loops runs till the length of array i.e. till 50 iterations.Inside each iteration value of each element is added to sum.
as all the elements has 0 value so there will not be any effect of adding 0 to any value.only 5th,10th and 15th location contain non-zero value.

i.e. sum=a[5]+a[10]+a[15]=5+10+15=30

--Sum is printed with value 30.
sum=30 wil get printed.

Posted by Mânïshå Mùlchåndânï    2015-01-13 11:28:27

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

Posted by Merit Campus    2015-01-19 04:58:53

© meritcampus 2019

All Rights Reserved.

Open In App