Menu
Topics Index
...
`

Arrays and Loops


If you need explanation Read this topic

If you need Answer Take test on this topic

Arrays and Loops
What will be the output of the following program?
class ArrayOutput
{
    public static void main(String arg[])
    {
        int[][] scores = new int[3][4];
        
        // in 2008 year
        scores[0][0] = 150;
        scores[0][1] = 170;
        scores[0][2] = 183;
        scores[0][3] = 216;
        
        // in 2009 year
        scores[1][0] = 258;
        scores[1][1] = 221;
        scores[1][2] = 241;
        scores[1][3] = 230;
        
        // in 2010 year
        scores[2][0] = 279;
        scores[2][1] = 260;
        scores[2][2] = 250;
        scores[2][3] = 274;
            
        System.out.println("India scored " + scores[2][0] + " in the year 2010.");
    
        int total_australia_score = scores[0][2] + scores[1][2] + scores[2][2];
          
        System.out.println("Australia scored " + total_australia_score + " in 3 years - 2008, 2009 and 2010.");
            
        int all_teams_2009 = scores[1][0] + scores[1][1] + scores[1][2] + scores[1][3];
            
        System.out.println("All teams scored " + all_teams_2009 + " in the year 2009.");
        
    }
}

A. India scored 279 in the year 2010.
Australia scored 674 in 3 years - 2008, 2009 and 2010.
All teams scored 950 in the year 2009.
B. India scored 230 in the year 2010.
Australia scored 674 in 3 years - 2008, 2009 and 2010.
All teams scored 950 in the year 2009.
C. India scored 279 in the year 2010.
Australia scored 900 in 3 years - 2008, 2009 and 2010.
All teams scored 950 in the year 2009.
D. Throws ArrayIndexOutOfBoundsException
Topic: Java Multidimensional Array

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 A..here we create the 2-D array with 3 rows and 4 cols..num of rows represents the length of an 2-d array
in the 1st row we are storing the values 150,170,183,216
in the next row we are assigning the 258,221,241,230
in the 3rd row we are storing 279,260,250,274
in the 1st sop we are printing the 1st element im the 2nd row which is 279
next we are adding the elements at the 2nd col in every row..which is 674
next we are adding the elements of the 1st row so sum is 950

Posted by Uday Kumar    2015-01-22 14:07:32


Ans is Option A... Execution starts from main() method after execution of first stmt it will create array of integer variable "scores" with size 3 rows and 4 coloumns after that every index in the matrix are intialized with the values like 1st row will store as 150 170 183 216  ans 2nd row as 258 221 241 230 and 3rd row as                  279 260 250 274  after that we are printing scores[2][0] means 1st element in 3rd row so it is 279 after that we are adding scores[0][2]+scores[1][2]+scores[2][2] and those values are 183+241+250=674 this value is printing in second println stmt after that we are adding values score[1][0]+scores[1][1]+scores[1][2]+scores[1][3] and these values are 258+221+241+230=950 it will print in 3rd println stmt so the Ans is Option A...

Posted by Bhagi Bhagyasri    2015-01-22 14:42:08


ans A

Posted by Shubham Bansal    2015-01-22 14:54:13


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Bhagi Bhagyasri,  for 'Best Comment' and Shubham Bansal for the 'Popular Comment'. The 'lucky liker' is Bhagi Bhagyasri. 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-23 05:06:44


© meritcampus 2019

All Rights Reserved.

Open In App