|
SCJP : Enhanced for loop (for-each) Mock Exam Practice Questions
Questions no -1
What is the output for the below code ?
public class Test {
public static void main(String... args) {
ArrayList list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
for(int i:list)
System.out.println(i);
}
}
options
A)1 2 3
B)Compile error , can't add primitive type in ArrayList
C)Compile error on for(int i:list) , Incorrect Syntax
D)0 0 0
Correct answer is : A
Explanations : JDK 1.5, 1.6 allows add primitive type in ArrayList and for(int i:list) syntax is also correct.
for(int i:list) is same as
for(int i=0; i
| |