Traversing ArrayLists

AP Computer Science A· difficulty 2/5

ArrayList<String> list = new ArrayList<>();
list.add("hi");
list.add("hello");
list.add("hey");
int total = 0;
for (String s : list) total += s.length();
System.out.println(total);

What is printed?

  • A

    10

    check_circle
  • B

    8

  • C

    11

  • D

    3

Explanation

"hi"=2, "hello"=5, "hey"=3. Sum = 2+5+3 = 10.

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

Related questions