Developing Algorithms Using ArrayLists

AP Computer Science A· difficulty 2/5

ArrayList<Integer> list = new ArrayList<>();
list.add(5);
list.add(2);
list.add(5);
list.add(7);
list.add(5);
list.add(2);
int count = 0;
for (int x : list) {
    if (x == 5) count++;
}
System.out.println(count);

What is printed?

  • A

    2

  • B

    4

  • C

    0

  • D

    3

    check_circle

Explanation

The value 5 appears at indices 0, 2, and 4. Count = 3.

Want 10 more like this — adaptive to your weak spots?

Related questions