Traversing ArrayLists

AP Computer Science A· difficulty 4/5

import java.util.ArrayList;
public static int run() {
    ArrayList<Integer> a = new ArrayList<>();
    a.add(3); a.add(1); a.add(4); a.add(1); a.add(5);
    int max = a.get(0);
    for (int i = 1; i < a.size(); i++) {
        if (a.get(i) > max) max = a.get(i);
    }
    return max;
}
// Call: System.out.println(run());

What is printed?

  • A

    4

  • B

    1

  • C

    3

  • D

    5

    check_circle

Explanation

Maximum of {3,1,4,1,5} is 5.

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

Related questions