241,282 views
35 votes
35 votes
The following segment of code is meant to remove the even numbers from an ArrayList list and print the results:

int counter = 0;
while(counter < list.size())
{
if(list.get(counter) %2 == 0)
{
list.remove(counter);
}
counter++;
}
System.out.println(list.toString());
The method as written, however, is incorrect. Which ArrayList(s) list would prove that this method was written incorrectly?

I.
[1, 2, 3, 4, 5]
II.
[2, 4, 5, 6, 7]
III.
[2, 4, 6, 8, 10]
IV.
[2, 5, 6, 7, 8]
III only


II and IV


II only


I and IV


II and III

User Grygoriy Gonchar
by
2.7k points

2 Answers

9 votes
9 votes

Answer:

II and III

Step-by-step explanation:

The following segment of code is meant to remove the even numbers from an ArrayList-example-1
User Henry He
by
3.1k points
27 votes
27 votes

Answer:

II and III

Step-by-step explanation:

I took the quiz and got this wrong, but it gives you the answer afterwards, just trying to help everyone else out.

User Paltoquet
by
3.4k points