136k views
3 votes
What is the output of the code?

import java.util.ArrayList;

public class Arraylist2
{
public static void main(String[] args){

ArrayList intList = new ArrayList ();

intList.add(10);
intList.add(20);
intList.add(30);

for(Integer item : intList) {
System.out.print(intList.get(0));
}
}
}

A. 101010
B. 102030
C. 10
D. 60

User Arnial
by
4.6k points

1 Answer

2 votes

Answer:

The correct answer to this question is given below in the explanation section.

Step-by-step explanation:

After running the program successfully, the output of this program is A. i.e. 101010.

Because when you run the given code:

import java.util.ArrayList;

public class Arraylist2

{

public static void main(String[] args){

ArrayList intList = new ArrayList ();

intList.add(10);

intList.add(20);

intList.add(30);

for(Integer item : intList) {

System.out.print(intList.get(0));

}

}

}

It add three values 10,20,and 30 in the arrayList variable intList at index 0,1, and 2 respectively. When you run this program, it iterates 3 times and prints the value at index zero that is 10 repetatively. so the output is A.

User Tracy
by
5.5k points