wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Ap computer science a

Total questions: 15

Worksheet time: 12mins

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.

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]

4.

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

5.

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)

6.

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)

7.

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

8.

What is printed as a result of executing the following code?

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

alist.add(1);

alist.add(2);

alist.remove(1);

alist.add(1, 3);

alist.set(1, 4);

alist.add(5);

System.out.println(alist);

a)

[1, 4, 5]

b)

[1, 4, 3, 5]

c)

[2, 4, 5]

d)

[2, 4, 3, 5]

9.

What is printed as a result of executing the following code segment?

ArrayList<Double> vals = new ArrayList<Double>();

vals.add(3.5);

vals.add(4.5);

vals.add(5.5);

vals.add( vals.set(0, 2.5) );

System.out.println(vals);

a)

[2.5, 4.5, 5.5, 3.5]

b)

[2.5, 3.5, 4.5, 5.5]

c)

[3.5, 4.5, 5.5, 2.5]

d)

[2.5, 4.5, 5.5]

10.

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>();

11.

What is printed as a result of executing the following code on an ArrayList of Integers called nums with the values [3, 2, -4, 5, 7]?

for (int idx = 0; idx < nums.size(); idx++)

if (nums.get(idx) % 2 == 0)

nums.add(99);

System.out.println(nums.size());

a)

7

b)

5

c)

6

d)

memory full because too many values added

12.
Which is correct syntax to access an element of an ArrayList in Java?
a)
list.get(index)
b)
list[index]
c)
list.getElement(index)
d)
list.index
13.
What does it mean for a list to be "dynamic"?
a)
You can easily add and remove elements
b)
It has a set size once it has been created
c)
It may contain multiple types of values
d)
You can access it from anywhere in the program
14.
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();
15.
The _________ method will return the total elements in an ArrayList.
a)
total()
b)
length()
c)
size()
d)
length