Developing Algorithms Using ArrayLists

AP Computer Science A· difficulty 2/5

ArrayList<Integer> a = new ArrayList<>();
a.add(1);
a.add(2);
a.add(3);
ArrayList<Integer> b = new ArrayList<>();
for (int x : a) b.add(x * 2);
System.out.println(b);

What is printed?

  • A

    [2, 4, 6]

    check_circle
  • B

    [1, 2, 3]

  • C

    [1, 4, 9]

  • D

    []

Explanation

Each element of a is doubled and added to b: 1<em>2=2, 2</em>2=4, 3*2=6.

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

Related questions