Question 161
ArrayList<Integer> a = new ArrayList<Integer>();
for (int i = 1; i <= 6; i++)
{
a.add(i * 3);
}
for (int i = 0; i < a.size(); i++)
{
if (i % 2 == 0)
{
a.set(i, a.get(i) + i);
}
}
System.out.println(a);
What is printed?Main Solution
Solution:
Correct: A. First loop fills [3, 6, 9, 12, 15, 18]. Second loop modifies only even indices by adding the index: i=0→3+0=3, i=2→9+2=11, i=4→15+4=19. Odd indices unchanged. Result: [3, 6, 11, 12, 19, 18].
Answer: Correct: A
Answer images

