Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Arrays and ArrayLists

Total questions: 15

Worksheet time: 34mins

Name
Class
Date
1.

Consider the following code segment. Which of the following statements best describes the condition when it returns true? a is an integer array.

a)

whenever the first element in a is equal to val

b)

Whenever a contains any element which equals val

c)

Whenever the last element in a is equal to val

d)

Whenever more than 1 element in a is equal to val

e)

Whenever exactly 1 element in a is equal to val

2.

Look at the following code segment. Which of the following arrays would return true when val = 4? Select all that apply.

a)

a = {4, 3, 2, 1};

b)

a = {1, 2, 3, 4};

c)

a = {1, 2, 3, 4, 3, 2, 1};

d)

a = {4, 4, 2, 2};

e)

a = {2, 2, 2, 4};

3.

Look at the following code. What would findLargest(10) return?

a)

2

b)

3

c)

5

d)

7

e)

13

4.

Consider the following field arr and method checkArray. Which of the following best describes what checkArray returns?

a)

Returns the index of the largest value in array arr.

b)

Returns the index of the first element in array arr whose value is greater than arr[loc].

c)

Returns the index of the last element in array arr whose value is greater than arr[loc].

d)

Returns the largest value in array arr.

e)

Returns the index of the largest value in the second half of array arr.

5.

Given the following field and method declaration, what is the value in a[1] after m1(a) has run?

a)

4

b)

2

c)

12

d)

6

e)

3

6.

Given the following code, what does m1(a) return?

a)

4

b)

2

c)

12

d)

6

e)

3

7.

Consider the following method changeArray. An array is created that contains {2, 8, 10, 9, 6} and is passed to changeArray. What are the contents of the array after the changeArray method executes?

a)

{2, 6, 2, -1, -3}

b)

{-23, -21, -13, -3, 6}

c)

{10, 18, 19, 15, 6}

d)

This method results in an IndexOutOfBounds exception.

e)

{35, 33, 25, 15, 6}

8.

Which index is the last element in a list called nums at?

a)

nums.length

b)

nums.length - 1

c)

nums.length()

d)

nums.size()

e)

nums.size() - 1

9.

Which of the following is a reason to use an ArrayList instead of an array?

a)

An ArrayList can grow or shrink as needed, while an array is always the same size.

b)

You can use a for-each loop on an ArrayList, but not in an array.

c)

You can store objects in an ArrayList, but not in an array.

10.

Which of the following is the correct way to get the first value in a list called nums?

a)

nums[0]

b)

nums[1]

c)

nums.first()

d)

nums.get(1)

e)

nums.get(0)

11.

Which of the following is the correct way to set the third value in a list called nums to 5?

a)

nums[2] = 5;

b)

nums[3] = 5;

c)

nums.set(5, 2);

d)

nums.set(2, 5);

e)

nums.set(3, 5);

12.

Which of the following is the correct way to remove the value 1 from the list nums = [5, 3, 2, 1]?

a)

nums.remove(3);

b)

nums.remove(1);

c)

nums.remove(2);

d)

nums.remove(4);

e)

nums.remove(nums.size());

13.

Which of the following is the correct way to add 2 between the 1 and 3 in the following list nums = [1, 3, 4]?

a)

nums.add(1, 2);

b)

nums.add(2, 0);

c)

nums.add(2, 1);

d)

nums.add(0, 2);

e)

nums.add(2, 2);

14.

What will print when the following code executes?

a)

[1, 3]

b)

[1, 2]

c)

[1, 2, 3]

d)

[2, 3]

15.

What will print when the following code executes?

a)

["Sarah", "Destini", "Layla", "Sharrie"]

b)

["Sarah", "Destini", "Anaya", "Layla", "Sharrie"]

c)

["Sarah", "Layla", "Sharrie"]

d)

["Destini", "Layla", "Sharrie", "Sarah"]