ArrayList: MCQ
1
Consider the following statement, which is intended to create an ArrayList named a to store only elements of type Thing.
Assume that the Thing class has been properly defined and includes a no-parameter constructor.
ArrayList<Thing> a = /* missing code */;
Which of the following can be used to replace /* missing code */ so that the statement works as intended?
A. new Thing()
B. new ArrayList<Thing>()
C. new ArrayList(Thing)
D. new ArrayList(<Thing>)
E. new ArrayList<>(Thing)
2
Consider the following statement, which is intended to create an ArrayList named numbers that can be used to store
Integer values.
ArrayList<Integer> numbers = /* missing code */;
Which of the following can be used to replace /* missing code */ so that the statement works as intended?
I new ArrayList()
II new ArrayList<Integer>
III new ArrayList<Integer>()
A. III only
B. I and II only
C. I and III only
D. II and III only
E. I, II, and III
3
Consider the following statement, which is intended to create an ArrayList named arrList to store elements only of type
String.
/* missing code */ = new ArrayList<String>();
Which of the following can be used to replace /* missing code */ so that the statement works as intended?
A. ArrayList arrList()
B. ArrayList arrList
C. ArrayList<> arrList
D. ArrayList arrList<String>
E. ArrayList<String> arrList
4
Consider the following code segment.
ArrayList<Integer> nums = new ArrayList<>();
[Link](3);
[Link](2);
[Link](1);
[Link](0);
[Link](0, 4);
[Link](3, 2);
[Link](3);
[Link](2, 0);
Which of the following represents the contents of nums after the code segment has been executed?
A. [2, 4, 3, 2, 0]
B. [3, 2, 0, 1, 0]
C. [4, 2, 0, 2, 0]
D. [4, 3, 0, 2, 0]
E. [4, 3, 0, 3, 0]
5
Consider the following code segment.
ArrayList<String> syllables = new ArrayList<String>();
[Link]("LA");
[Link](0, "DI");
[Link](1, "TU");
[Link]("DA");
[Link](2, [Link](0));
[Link](1);
[Link]([Link]());
What is printed as a result of executing the code segment?
A. [DI, DA, DI]
B. [DI, DI, DA]
C. [LA, LA, DA]
D. [TU, DI, DA]
E. [TU, TU, DA]
6
Consider the following code segment.
ArrayList<Integer> vals = new ArrayList<Integer>();
[Link]([Link](), [Link]());
[Link]([Link]() - 1, [Link]() + 1);
[Link]([Link]() - 2, [Link]() + 2);
[Link]([Link]());
What is printed as a result of executing the code segment?
A. [0, 1, 2]
B. [0, 2, 4]
C. [1, 2, 3]
D. [2, 1, 0]
E. [4, 2, 0]
7
Consider the following code segment.
ArrayList<Integer> myList = new ArrayList();
for (int i = 0; i < 4; i++)
{
[Link](i + 1);
}
for (int i = 0; i < 4; i++)
{
if (i % 2 == 0)
{
[Link]([Link](i) + " ");
}
}
What output is produced as a result of executing the code segment?
A. 0123
B. 1234
C. 02
D. 13
E. 24
8
Consider the following code segment.
ArrayList<String> words = new ArrayList<String>();
[Link]("mat");
[Link]("new");
[Link]("open");
[Link]("pet");
int i = 0;
while (i < [Link]())
{
[Link](i);
i++;
}
[Link]([Link]());
What is printed when the code segment is executed?
A. []
B. [new, pet]
C. [open, pet]
D. [new, open, pet]
E. [mat, new, open, pet]
9
Consider the following code segment.
ArrayList<String> arrList = new ArrayList<String>();
[Link]("A");
[Link]("B");
[Link]("C");
[Link]("D");
for (int i = 0; i < [Link](); i++)
{
[Link]([Link](i));
}
What, if anything, is printed as a result of executing the code segment?
A. AC
B. BD
C. ABC
D. ABCD
E. Nothing is printed.
10
Consider the following method definition. The method isReversed is intended to return true if firstList and
secondList contain the same elements but in reverse order, and to return false otherwise.
/** Precondition: [Link]() == [Link]() */
public static boolean isReversed(ArrayList<Integer> firstList, ArrayList<Integer>
secondList)
{
for (int j = 0; j < [Link]() / 2; j++)
{
if([Link](j)
!=[Link]([Link]() - 1 - j))
{
return false;
}
}
return true;
}
The method does not always work as intended. For which of the following inputs does the method NOT return the correct
value?
A. When firstList is {1, 3, 3, 1} and secondList is {1, 3, 3, 1}
B. When firstList is {1, 3, 3, 1} and secondList is {3, 1, 1, 3}
C. When firstList is {1, 3, 5, 7} and secondList is {5, 5, 3, 1}
D. When firstList is {1, 3, 5, 7} and secondList is {7, 5, 3, 1}
E. When firstList is {1, 3, 5, 7} and secondList is {7, 5, 3, 3}
11
In the code segment below, myList is an ArrayList of integers. The code segment is intended to remove all elements
with the value 0 from myList.
int j = 0;
while (j < [Link]())
{
if ([Link](j) == 0)
{
[Link](j);
}
j++;
}
The code segment does not always work as intended. For which of the following lists does the code segment NOT
produce the correct result?
A. {0, 1, 2, 3}
B. {0, 1, 0, 2}
C. {1, 0, 0, 2}
D. {1, 2, 3, 0}
E. {1, 2, 3, 4}
12
In the code segment below, numList is an ArrayList of integers that is sorted in descending order. The code
segment is intended to insert the integer value val into numList so that numList is still sorted in descending
order.
int j = 0;
while (val != [Link](j))
{
j++;
}
[Link](j, val);
The code segment does not always work as intended. Assuming that numList has been initialized to {3, 2, 1,
0}, for which value of val does the code segment NOT produce the expected result?
A. 4
B. 3
C. 2
D. 1
E. 0
13
Consider the method sequentialSearch, which takes an ArrayList of Integer elements and an int value as
parameters and returns the index of the first appearance of the target value in the list or -1 if the target value does not
appear in the list.
public static int sequentialSearch(ArrayList<Integer>
elements, int target)
{
for (int j = 0; j < [Link](); j++) // Line 3
{
if ([Link](j) == target)
{
return j;
}
}
return -1;
}
Which of the following explains how replacing Line 3 with for (int j = ([Link]() - 1); j >= 0; j--) will affect the
behavior of sequentialSearch?
A. The modification has no effect: the modified method will continue to return the index of the first appearance of the
target value in the list, or -1 if the target value does not appear in the list.
B. The modified method will return the index of the last appearance of the target value in the list, or -1 if the target value
does not appear in the list.
C. The modified method will throw an ArrayIndexOutOfBoundsException.
D. The modified method will return -1 regardless of the inputs.
E. The modified method will not compile.
14
Consider the following search method.
public static int search(int[] arr, int target)
{
int result = -1;
for (int j = 0; j < [Link]; j++)
{
if (arr[j] == target)
{
result = j; // Line 8
}
}
return result;
}
Which of the following describes the effect of replacing the statement in line 8 of the method with result =
arr[j]; ?
A. The modified method will return the index of the first occurrence of target in arr.
B. The modified method will return the index of the last occurrence of target in arr.
C. The modified method will return target if target appears in arr and will return -1 otherwise.
D. The modified method will return -1 if target appears in arr and will return target otherwise.
E. The modified method will return -1 for all possible inputs.
15
Consider the method seqSearch, which implements a sequential search algorithm.
public int seqSearch(int[] arr, int target)
{
for (int j = 0; j < [Link]; j++)
{
if (arr[j] == target)
{
return j;
}
}
return -1;
}
Consider another method, seqSearch2, which modifies seqSearch to use an enhanced for loop.
public int seqSearch2(int[] arr, int target)
{
for (int j : arr)
{
if (j == target)
{
return j;
}
}
return -1;
}
Which of the following best describes the difference in the behavior of seqSearch2 relative to seqSearch as a result of the
modification?
A. The modification in seqSearch2 has no effect: seqSearch2 will always behave exactly as seqSearch does.
B. The modification in seqSearch2 will cause a compilation error.
C. The modification in seqSearch2 will cause an ArrayIndexOutOfBoundsException to be thrown for some inputs.
D. The modification in seqSearch2 will cause -1 to be returned for all inputs.
E. The modification in seqSearch2 will cause the value of target to be returned instead of the index of target in cases
where target appears in arr.
16
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<Integer> arr)
{
int count = 0;
for (int j = 0; j < [Link](); j++) // Line 4
{
if ([Link](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 <= [Link]() – 1?
(A) It has no impact on the behavior of the method.
(B) It causes the method to ignore the last element in arr.
(C) It causes the method to throw an indexOutOf Bounds exception.
(D) It reduces the size of arr by 1 and the last element will be removed.
(E) It changes the number of times the loop executes, but all indexes in arr will still be accessed.
17
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<String> arrz String key)
{
for (int j = 0; j < [Link](); j++) // Line 3
{
if ([Link](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 ?
(A) It has no impact on the behavior of the method.
(B) It will cause the method to return a different result when the key value is not in the list.
(C) It will cause the method to return a different result when the key value is found only at the first index in the list.
(D) It will cause the method to return a different result when the key value is found only at the last index in the list.
(E) It will cause the method to throw an array index out of bounds exception.
18
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<Integer> a, ArrayList<Integer> b) {
for (int i = 0; i < [Link](); i++)
{
for (int j = 0; j < [Link](); j++) // Line 5
{
if ([Link](i).equals([Link](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 =
[Link]() - 1; j > 0; j--) ?
(A) The change has no impact on the behavior of the method.
(B) After the change, the method will never check the first element in list b.
(C) After the change, the method will never check the last element in list b.
(D) After the change, the method will never check the first and the last elements in list b.
(E) The change will cause the method to throw an indexOutof Bounds exception.