wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

ArrayList

Total questions: 11

Worksheet time: 8mins

Name
Class
Date
1.

What is the list nums if it is initially [5, 3, 1] and the following code is executed?

nums.add(6);

nums.add(0, 4);

nums.remove(1);

a)

[4, 3, 1, 6]

b)

[5, 3, 1, 6]

c)

[4, 3, 6]

d)

[4, 5, 3, 6]

2.

What index value is used to locate the last element in the nums ArrayList?

a)

nums.size()-1

b)

nums.length()-1

c)

nums.size()

d)

nums.length

3.

What will print when the following code executes?

ArrayList<Integer> list = new ArrayList<Integer>();

list.add(7);

list.add(8);

list.add(9);

System.out.println(list.remove(0));

a)

7

b)

8

c)

9

d)

0

4.

Which statement is the correct declaration and initialization of an ArrayList of String values?

a)

ArrayList<String> name;

name = new ArrayList<String>();

b)

ArrayList name;

name = new ArrayList<String>();

c)

ArrayList<String> name;

name = ArrayList<String>();

d)

String<ArrayList> name;

name = new String<ArrayList>();

5.
How do you get the number of elements in an ArrayList called "list" in Java?
a)
list.size();
b)
list.count();
c)
list.length();
d)
list.getSize();
6.

Which statement below is the correct way to retrieve the first element in the nums ArrayList?

a)

nums.get(0)

b)

nums[0]

c)

nums(0)

d)

nums[1]

7.
_________________ is a collection of elements used to store the same type of data.
a)
Array
b)
Switch
c)
Case
d)
Loop
8.
How do you add an element with the value "Articuno" to the back of an ArrayList called "Pokemon" in Java?
a)
Pokemon.add("Articuno");
b)
Pokemon.append("Articuno");
c)
Pokemon.push_back("Articuno");
d)
Pokemon[last_index + 1] = "Articuno";
9.

Given the ArrayList nums with the values [3, 7, 6, 0], what code below is the proper way to change the 7 to be a 5?

a)

nums.set(1, 5)

b)

nums.set(7, 5)

c)

nums.set(5, 2)

d)

nums[2] = 5

10.

Given the nums ArrayList with the values [5, 4, 3, 2], what statement below removes the value 3 from the list?

a)

nums.remove(2)

b)

nums.remove(3)

c)

nums.get(2) = null

d)

nums.remove(1)

11.

Given the nums ArrayList with the values [1, 2, 3, 4, 6], what code below can be used to add a 5 between the 4 and 6 in the list?

a)

nums.add(4, 5)

b)

nums.add(5, 4)

c)

nums.add(5, 5)

d)

nums.add(5)