Unit+7+Topic+Questions+-+ArrayList
Unit+7+Topic+Questions+-+ArrayList
4. Consider the following statement, which is intended to create an ArrayList named theater_club to store
elements of type Student. Assume that the Student class has been properly defined and includes a no-
parameter constructor.
Which choice can replace /* missing code */ so that the statement compiles without error?
(A) Student()
(B) Student ArrayList()
(C) ArrayList(Student)
(D) ArrayList<Student>()
(E) ArrayList<theater_club>()
5. 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.
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 ?
(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 IndexOutOfBounds 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.
6. 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.
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 ?
7. 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.
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--) ?
(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 IndexOutOfBounds exception.
8. 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);
9. 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, 5, 6},
then after executing remDups(nums), nums should contain {1, 2, 3, 4, 3, 5, 6}.
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?
(A) {1, 1, 2, 3, 3, 4, 5}
(B) {1, 2, 2, 3, 3, 4, 5}
(C) {1, 2, 2, 3, 4, 4, 5}
(D) {1, 2, 2, 3, 4, 5, 5}
(E) {1, 2, 3, 3, 4, 5, 5}
10. The removeElement method is intended to remove all instances of target from the ArrayList object
data passed as a parameter. The method does not work as intended for all inputs.
Assume that the ArrayList object scores and the int variable low_score have been properly
declared and initialized. In which of the following cases will the method call removeElement(scores,
low_score) fail to produce the intended result?
(A) When scores is [0, 2, 0, 2, 0, 6] and low_score is 0
(B) When scores is [2, 4, 0, 5, 7, 0] and low_score is 0
(C) When scores is [3, 4, 5, 7, 7, 2] and low_score is 1
(D) When scores is [8, 8, 4, 3, 3, 6] and low_score is 3
(E) When scores is [9, 9, 5, 9, 7, 7] and low_score is 5
11. In the following code segment, assume that the ArrayList numList has been properly declared and
initialized to contain the Integer values [1, 2, 2, 3]. The code segment is intended to insert the
Integer value val in numList so that numList will remain in ascending order. The code segment does
not work as intended in all cases.
int index = 0;
while (val > numList.get(index))
{
index++;
}
numList.add(index, val);
For which of the following values of val will the code segment not work as intended?
(A) 0
(B) 1
(C) 2
(D) 3
(E) 4
12. Consider the following correct implementation of the selection sort algorithm.
The following declaration and method call appear in a method in the same class as selectionSort.
How many times is the statement elements[minIndex] = temp; in line 19 of the method executed as a
result of the call to selectionSort ?
(A) 1
(B) 2
(C) 3
(D) 4
(E) 5
13. Consider the following statement, which is intended to create an ArrayList named values that can be used
to store Integer elements.
Which of the following can be used to replace /* missing code */ so that the statement compiles without error?
I. ArrayList values
II. ArrayList<int> values
III. ArrayList<Integer> values
(A) I only
(B) II only
(C) III only
(D) I and III only
(E) II and III only
14. Consider the following statement, which is intended to create an ArrayList named years that can be used to
store elements both of type Integer and of type String.
Which of the following can be used to replace /* missing code */ so that the statement compiles without error?
(A) ArrayList years
(B) ArrayList years()
(C) ArrayList years[]
(D) ArrayList<Integer> years
(E) ArrayList<String> years
15. Consider the following correct implementation of the insertion sort algorithm.
{
elements[possibleIndex] = elements[possibleIndex - 1];
possibleIndex--; // Line 10
}
elements[possibleIndex] = temp;
}
}
The following declaration and method call appear in a method in the same class as insertionSort.
How many times is the statement possibleIndex--; in line 10 of the method executed as a result of the call
to insertionSort ?
(A) 2
(B) 3
(C) 4
(D) 5
(E) 6
16. Consider the following correct implementation of the selection sort algorithm.
The following declaration and method call appear in the same class as selectionSort.
How many times is the statement minIndex = k; in line 11 of the method executed as a result of the call to
selectionSort ?
(A) 0
(B) 1
(C) 2
(D) 3
(E) 4
17. In the following code segment, assume that the ArrayList data has been initialized to contain the
Integer values [4, 3, 4, 5, 3, 4].
int j = 0;
while (j < data.size() - 1)
{
if (data.get(j) > data.get(j + 1))
{
System.out.print(data.get(j + 1) + " ");
}
j++;
}
18. In the code segment below, assume that the ArrayList object numbers has been properly declared and
initialized to contain [0, 2, 4, 5].
19. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are
called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined
in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will
not receive full credit.
This question involves the scheduling of car repairs. The classes used in the question are used to record information
about car repairs. The methods shown and the methods to be written involve the mechanic performing a repair and the
bay in which the repair takes place. A bay is an area of a repair shop in which a car is parked while a mechanic
performs a repair. Mechanics and bays are identified by sequential integer identification numbers that start with 0.
The following RepairSchedule class represents the use of bays by mechanics repairing cars. You will write two
methods of the RepairSchedule class.
(a) Write the addRepair method. The method attempts to schedule a repair by the mechanic with identifier m in the
bay with identifier b. The repair can be scheduled if mechanic m and bay b are both available. A mechanic is available
if the given mechanic number does not appear in an element of schedule and a bay is available if the given bay
number does not appear in an element of schedule.
If the mechanic and bay are both available, the addRepair method adds the repair to schedule and returns true.
If either the mechanic or the bay are not available, the addRepair method returns false.
The following sequence of statements provides examples of the behavior of the addRepair method.
The call r.addRepair(0, 1) returns true because neither mechanic 0 nor bay 1 are present in schedule.
The contents of schedule after the call are as follows.
The call r.addRepair(0, 2) returns false because mechanic 0 is present in schedule. The contents of
schedule after the call are as follows.
The call r.addRepair(2, 4) returns false because bay 4 is present in schedule. The contents of
schedule after the call are as follows.
The call r.carOut(4) removes the repair in bay 4 from schedule. The carOut method is shown here to
illustrate that bays and mechanics become available when car repairs are complete. You do not need to write or
call this method. The contents of schedule after the call are as follows.
The call r.addRepair(1, 4) returns true because neither mechanic 1 nor bay 4 are present in schedule.
The contents of schedule after the call are as follows.
(b) Write the availableMechanics method, which returns an ArrayList containing the mechanic numbers of
all available mechanics. If there is no available mechanic, an empty list is returned. A mechanic is available if the
mechanic’s identifier does not appear in schedule. Suppose schedule has the following contents.
For these contents of schedule, availableMechanic should return an ArrayList containing the values 2, 3,
4, and 5 (in any order).
Complete the availableMechanics method. Assume that addRepair works as specified, regardless of what you
wrote in part (a).