Developing Algorithms Using ArrayLists

AP Computer Science A· difficulty 3/5

ArrayList<Integer> list = new ArrayList<>();
list.add(3);
list.add(7);
list.add(2);
list.add(8);
list.add(5);
int max = list.get(0);
for (int i = 1; i < list.size(); i++) {
    if (list.get(i) > max) max = list.get(i);
}
System.out.println(max);

What is printed?

  • A

    7

  • B

    8

    check_circle
  • C

    3

  • D

    5

Explanation

Standard find-max algorithm. The maximum value in [3, 7, 2, 8, 5] is 8.

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

Related questions