Menu
Topics Index
...
`

Array Accessing755


If you need explanation Read this topic

If you need Answer Take test on this topic

Array Accessing755
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 = 2 * 2;
        int k = i - j;
    
        a[i] = j;
        a[j] = i;
        a[k] = k;
    
        int sum = 0;
    
        for(int l = 0; l < a.length; l++)
        {
            sum += a[k];
        }
        System.out.println("Sum = " + sum);
    }
}

A. Sum = 10
B. Sum = 30
C. Sum = 50
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 we have the Arrays concept..arrays can store similar kind of elements in adjacent memory locations and arrays are fixed in the size..once we define the array size than we can not modify it..

Execution:
here we create the int array with the size 50 so here memory is allocated for the 50 integer blocks and initially all block with 0's ..and  we create the 3 local variables i,j,k
here i=27%11 means % will give the reminder so i is assigned with the value 5 and j=2*2 which is 4 and k=i-j means 1;
and we are storing the values in i,j,k locations 5,4,1.
next we take another local variable sum=0..we have an for loop to travel through the array..while traveling we are adding the array[k] to the sum every time here array[k] is 1 so value of array[k] is added 50 times to the sum...sum values is 50 after coming out of the loop

Posted by Uday Kumar    2015-01-26 15:05:29


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 used.so,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            f  alse
____________________________________________________

--So,Here 50 integer elements will be initialized with value 0.
--After that i=27%11 ---> i=5
        j=2*2---> j=4
        k=*j-i=5-4----> k=1

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

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

--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+4+1=10

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

Posted by Mânïshå Mùlchåndânï    2015-01-26 15:10:29


c

Posted by Shubham Bansal    2015-01-26 15:14:23


Ans is : C

ArrayOutput is a class with an array iof size 50.
i=27%11; in this statemaent i=5.
j=2*2; so j=4.
k=i-j so 5-4 i.e k=1
now
a[i]=j;  i.e
a[4]=5 and a[1]=1 are assigned.
now sum is declared and assigned a value 0 i.e sum=0.
here for loop  l=0 to l<50
here sum+=a[k]; i.e sum is incrementing 1 every time from 0 to 49
so now sum=50;

Posted by Ananthoju Arun Chary    2015-01-26 15:56:44


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar, Ananthoju Arun Chary,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Sai Ram. 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-01-27 07:49:48


© meritcampus 2019

All Rights Reserved.

Open In App