Introduction to ArrayList

AP Computer Science A· difficulty 2/5

ArrayList<Integer> list = new ArrayList<>();
int sum = 0;
for (int x : list) sum += x;
System.out.println(sum + " " + list.size());

What is printed?

  • A

    Runtime error

  • B

    0 1

  • C

    1 0

  • D

    0 0

    check_circle

Explanation

The list is empty so the for-each loop never executes, sum stays 0, and size is 0.

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

Related questions