ArrayList Methods

AP Computer Science A· difficulty 4/5

Before: AB CD After ops: [X, A, B, Y, C, D]

Starting with <code>a = ["A","B","C","D"]</code>:

<code class="language-java">a.add(0, "X"); a.add(3, "Y"); a.remove("B"); </code></pre> What is the final state of <code>a</code>? (<code>remove(Object)</code> removes the first matching element.)

  • A

    [A, X, Y, C, D]

  • B

    [X, A, Y, B, C, D]

  • C

    [X, A, B, Y, C, D]

  • D

    [X, A, Y, C, D]

    check_circle

Explanation

After add(0,"X"): [X,A,B,C,D]. After add(3,"Y"): [X,A,B,Y,C,D]. remove("B") removes the first "B": [X,A,Y,C,D].

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

Related questions