Developing Algorithms Using ArrayLists

AP Computer Science A· difficulty 3/5

ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
int run = 0;
ArrayList<Integer> result = new ArrayList<>();
for (int x : list) {
    run += x;
    result.add(run);
}
System.out.println(result);

What is printed?

  • A

    [1, 3, 6, 10]

    check_circle
  • B

    [1, 2, 3, 4]

  • C

    [10, 6, 3, 1]

  • D

    [0, 1, 3, 6]

Explanation

Running sums: 1, 1+2=3, 3+3=6, 6+4=10.

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

Related questions