Topics Index

Foreach loop for Arrays

If you need explanation Read this topic

If you need Answer Take test on this topic

Foreach loop for Arrays
What will be the output of the following program?
class forEachloop
    public static void main(String args[])
        int[] scores = {215, 234, 218, 189, 221, 290};
        int score = 0;
        for(score : scores)
            System.out.print(scores + "  ");

A. 215 234 218 189 221 290
B. Prints the reference values
C. Compilation Error 
D. Throws ArrayIndexOutOfBoundsException
Topic: For-each Loop In Core Java Programming

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 have the for each loop concept which is extension to the for loop..when we are using for loops we may set wrong condition like i=0 r i=1 i<=size r i<size this may cause the exceptions...using for each loop we can travel through the array in safe manner
EX: for(target variable: arrayname)

here we should create the target variable if array is int than target variable is int..variable declaration must do here only..we can use these loops for nesting purpsoe we are using the score which is declared outside so here compiler will raise an variables must create inside the loop

Posted by Uday Kumar    2015-01-27 03:53:18

Ans is Option C... Execution starts from main() method here we are creating array of integer variable scores and initialize with the values 215,234,218,189,221,290 and we are creating variable score initialized with the value 0 after that we are writing for-each loop here we are using score variable as a target iterator here we will get error create local variable bcz for for-each loop we have to create target variable as local variable i.e for(datatype variable:arrayname)  and also it will give error as "cannot convert from element type int to score" so Ans is C

Posted by Bhagi Bhagyasri    2015-01-27 04:24:28

ans is C  here execution starts from main() and scores array with some set of values is initialized  and score=0 is initialized .in the for loop ie for(score:scores) here as per for loop is concerned        for(declaration : expression)   {             //Statements                                                           } here in declaration, the newly declared block variable, which is of a type compatible with the elements of the array you are accessing. ie local variable of type compatable with scores is created an   its value would be the same as the current array element. so here score should not used .therefore it leads to a compilation error

Posted by Ramya Reddy Addulla    2015-01-27 12:31:20


Posted by Gaurav Bansal    2015-01-27 19:39:16

This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Bhagi Bhagyasri,  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-28 03:40:21

© meritcampus 2019

All Rights Reserved.

Open In App