AP Computer Science A · Topic 7.2
ArrayList Methods Practice
Part of ArrayList.
Practice questions
31
Sample questions
5 of 31 — sign in to practice the rest with adaptive difficulty and mastery tracking.
Sample 1difficulty 2/5
for (int i = 1; i <= list.size(); i++) { System.out.println(list.get(i)); }What is the bug?
- A
ArrayList lacks a get method
- B
Nothing is wrong
- C
println cannot accept an Object
- Dcheck_circle
Indices start at 0 and end at size()-1; should be i = 0; i < list.size()
Why
Java collections are zero-indexed. Starting at 1 skips the first element and going through size() throws IndexOutOfBoundsException.
- A
Sample 2difficulty 2/5
ArrayList<String> list = new ArrayList<>(); list.add("a"); list.add("b"); list.add("c"); list.remove(1); System.out.println(list.size());What is printed?
- Acheck_circle
2
- B
1
- C
3
- D
0
Why
Three elements are added, then one is removed. The size is now 3 - 1 = 2.
- A
Sample 3difficulty 2/5
Get element at index 2 of an ArrayList:
- Acheck_circle
list.get(2)
- B
list.at(2)
- C
list[2]
- D
list.element(2)
Why
ArrayList uses methods, not bracket notation.
- A
Sample 4difficulty 2/5
ArrayList<Integer> list = new ArrayList<>(); list.add(10); list.add(20); list.add(30); list.add(40); int sum = list.get(0) + list.get(list.size() - 1); System.out.println(sum);What is printed?
- A
60
- B
30
- Ccheck_circle
50
- D
100
Why
First element 10 plus last element 40 equals 50.
- A
Sample 5difficulty 2/5
ArrayList<Integer> list = new ArrayList<>(); list.add(10); list.add(20); list.add(30); list.set(1, 99); System.out.println(list.get(1));What is printed?
- A
30
- B
10
- C
20
- Dcheck_circle
99
Why
set(1, 99) replaces the element at index 1 with 99. get(1) returns the new value 99.
- A