ArrayList Quiz

ArrayList Quiz

12th Grade

16 Qs

quiz-placeholder

Similar activities

Data Structures

Data Structures

12th Grade

15 Qs

Unit 6 ArrayLists and String Methods

Unit 6 ArrayLists and String Methods

9th - 12th Grade

20 Qs

Unit 7 - ArrayList - Test Review

Unit 7 - ArrayList - Test Review

9th - 12th Grade

12 Qs

Javascript Arrays

Javascript Arrays

9th - 12th Grade

12 Qs

Basic Data Structures - Intro to CS

Basic Data Structures - Intro to CS

12th Grade

15 Qs

Java Arrays

Java Arrays

9th - 12th Grade

20 Qs

Arrays and ArrayLists

Arrays and ArrayLists

10th - 12th Grade

15 Qs

JDVP Activity

JDVP Activity

12th Grade - University

20 Qs

ArrayList Quiz

ArrayList Quiz

Assessment

Quiz

Computers

12th Grade

Hard

Created by

brent hopkins

Used 3+ times

FREE Resource

16 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Consider the following code segment. ArrayList oldList = new ArrayList(); oldList.add(100); oldList.add(200); oldList.add(300); oldList.add(400); ArrayList newList = new ArrayList(); newList.add(oldList.remove(1)); newList.add(oldList.get(2)); System.out.println(newList); What, if anything, is printed as a result of executing the code segment?

[100, 300, 400]

[200, 300]

[200, 400]

Nothing is printed because the code segment does not compile.

Nothing is printed because an IndexOutOfBoundsException will occur.

2.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Consider the following method countNegatives, which searches an ArrayList of Integer objects and returns the number of elements in the list that are less than 0. public static int countNegatives(ArrayList arr) { int count = 0; for (int j = 0; j < arr.size(); j++) // Line 4 { if (arr.get(j) < 0) { count++; } } return count; } Which of the following best explains the impact to the countNegatives method when, in line 4, j < arr.size() is replaced with j <= arr.size() - 1?

It has no impact on the behavior of the method.

It causes the method to ignore the last element in arr.

It causes the method to throw an IndexOutOfBoundsException.

It reduces the size of arr by 1 and the last element will be removed.

It changes the number of times the loop executes, but all indexes in arr will still be accessed.

3.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Consider the following method findValue, which takes an ArrayList of String elements and a String value as parameters and returns true if the String value is found in the list and false otherwise. public static boolean findValue(ArrayList arr, String key) { for (int j = 0; j < arr.size(); j++) // Line 3 { if (arr.get(j).equals(key)) { return true; } } return false; } Which of the following best explains the impact to the findValue method when, in line 3, int j = 0 is replaced by int j = 1 ?

It has no impact on the behavior of the method.

It will cause the method to return a different result when the key value is not in the list.

It will cause the method to return a different result when the key value is found only at the first index in the list.

It will cause the method to return a different result when the key value is found only at the last index in the list.

It will cause the method to throw an array index out of bounds exception.

4.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Consider the following method, inCommon, which takes two Integer ArrayList parameters. The method returns true if the same integer value appears in both lists at least one time, and false otherwise. public static boolean inCommon(ArrayList a, ArrayList b) { for (int i = 0; i < a.size(); i++) { for (int j = 0; j < b.size(); j++) // Line 5 { if (a.get(i).equals(b.get(j))) { return true; } } } return false; } Which of the following best explains the impact to the inCommon method when line 5 is replaced by for (int j = b.size() - 1; j > 0; j--) ?

It has no impact on the behavior of the method.

It will cause the method to return a different result when the common value is not in the list.

It will cause the method to return a different result when the common value is found only at the first index in the list.

It will cause the method to return a different result when the common value is found only at the last index in the list.

It will cause the method to throw an array index out of bounds exception.

5.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

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

Alex Alex Alex

Alex Alex Alex Alex Bob Carl

Alex Bob Carl Alex Alex Alex

Alex Bob Carl Alex Bob Carl

Nothing is printed because the first print statement will cause a runtime exception to be thrown.

6.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

In the following code segment, assume that the ArrayList wordList has been initialized to contain the String values ["apple", "banana", "coconut", "lemon", "orange", "pear"]. int count = 0; for (String word : wordList) { if (word.indexOf("a") >= 0) { count++; } } System.out.println(count); What is printed as a result of executing the code segment?

1

2

3

4

5

7.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Consider the following method, remDups, which is intended to remove duplicate consecutive elements from nums, an ArrayList of integers. For example, if nums contains [1, 2, 2, 3, 4, 3, 5, 6], then after executing remDups(nums), nums should contain [1, 2, 3, 4, 3, 5, 6]. public static void remDups(ArrayList nums) { for (int j = 0; j < nums.size() - 1; j++) { if (nums.get(j).equals(nums.get(j + 1))) { nums.remove(j); j++; } } } The code does not always work as intended. Which of the following lists can be passed to remDups to show that the method does NOT work as intended?

[1, 1, 2, 3, 3, 4, 5]

[1, 2, 2, 3, 3, 4, 5]

[1, 2, 2, 3, 4, 4, 5]

[1, 2, 2, 3, 4, 5, 5]

[1, 2, 3, 3, 4, 5, 5]

Create a free account and access millions of resources

Create resources
Host any resource
Get auto-graded reports
or continue with
Microsoft
Apple
Others
By signing up, you agree to our Terms of Service & Privacy Policy
Already have an account?