ArrayList<Integer> list = new ArrayList<>();
for (int i = 1; i <= 5; i++) list.add(i);
int s = 0;
for (int x : list) s += x;
System.out.println(s);What is printed?
- A
Compile-time error
- B
5
- C
10
- Dcheck_circle
15
Explanation
The loop adds 1, 2, 3, 4, 5 to the list. Summing them with the for-each loop gives 1+2+3+4+5 = 15. Autoboxing converts each int to an Integer when adding.