Section4.34.2ArrayList Medium Multiple Choice Questions
These problems are like those you will see on the AP CSA exam.
Activity4.34.1.
What is printed as a result of executing the following code segment?
List<Integer> list1 = new ArrayList<Integer>();
list1.add(new Integer(1));
list1.add(new Integer(2));
list1.add(new Integer(3));
list1.set(2, new Integer(4));
list1.add(2, new Integer(5));
list1.add(new Integer(6));
System.out.println(list1);
[1, 2, 3, 4, 5]
The set replaces the 3 at index 2 with the 4 so this can’t be right.
[1, 2, 4, 5, 6]
The add with an index of 2 and a value of 5 adds the 5 at index 2 not 3. Remember that the first index is 0.
[1, 2, 5, 4, 6]
The add method that takes just an object as a parameter adds that object to the end of the list. The set replaces the value at that index with the new value. The add with parameters of an index and an object puts the passed object at that index and moves any existing values by one index to the right (increments the index).
[1, 5, 2, 4, 6]
The add with an index of 2 and a value of 5 adds the 5 at index 2 not 1. Remember that the first index is 0.
You can step through the code above by clicking on this link Example-8-12-1 1
Given the following code and assume that nums initially contains [0, 0, 4, 2, 5, 0, 3], what will nums contain as a result of executing numQuest?
private List<Integer> nums;
// precondition: nums.size() > 0;
// nums contains Integer objects
public void numQuest()
{
int k = 0;
Integer zero = new Integer(0);
while (k < nums.size())
{
if (nums.get(k).equals(zero))
nums.remove(k);
else
k++;
}
}
[0, 4, 2, 5, 3]
This code will loop through the array list and if the current value at the current index (k) is 0 it will remove it. When you remove a value from an array list it moves all values to the right of that one to the left. It only increments the index when it doesn’t find a zero so it work work correctly.
[3, 5, 2, 4, 0, 0, 0]
This shows all zeros at the end and this code removes 0’s so this can’t be right.
[0, 0, 0, 4, 2, 5, 3]
This shows all zeros at the beginning and this code removes zeros so this can’t be right.
[4, 2, 5, 3]
This shows all zeros removed. Since k is only incremented if a value wasn’t removed this will work correctly.
[0, 0, 4, 2, 5, 0, 3]
This shows the original values, but this code does remove some zeros so this can’t be right.
You can step through the code above by clicking on this link Example-8-12-2 2
Which of the following best describes the behavior of process1 and process2 (shown below)?
public static List<Integer> process1(int n)
{
List<Integer> someList = new ArrayList<Integer>();
for (int k = 0; k < n; k++)
someList.add(k);
return someList;
}
public static List<Integer> process2(int n)
{
List<Integer> someList = new ArrayList<Integer>();
for (int k = 0; k < n; k++)
someList.add(k, k);
return someList;
}
Both methods produce the same result, and process1 is faster than process2.
In this case they do the same thing. The only difference would be if there were values in the list in process2.
The two methods produce different results and take the same amount of time.
These produce the same result on an empty list when you add to the end.
The two methods produce different results, and process1 is faster than process2.
These produce the same result on an empty list when you add to the end.
The two methods produce different results, and process2 is faster than process1.
These produce the same result on an empty list when you add to the end.
Both methods produce the same result and take the same amount of time.
The method process1 adds to the end of the list each time through the loop. The method process2 also adds to the end of the list each time through the loop. The only difference would be if there were values in the list in process2. Any existing values would be moved to the right. But, there are no existing values in the list at that index or beyond.
You can step through the code above by clicking on the link Example-8-12-3 3
What is printed as a result of executing the following code segment?
List<Integer> aList = new ArrayList<Integer>();
aList.add(new Integer(1));
aList.add(new Integer(2));
aList.add(1, new Integer(5));
aList.set(1, new Integer(4));
aList.add(new Integer(6));
aList.add(new Integer(3));
System.out.println(aList);
[1, 2, 5, 4, 6, 3]
The set replaces the 3 with the 4 so this can’t be right.
[6, 5, 4, 3, 2, 1]
The add with an index of 2 and a value of 5 adds the 5 at index 2 not 3. Remember that the first index is 0.
[1, 2, 3, 4, 5, 6]
The add method that takes just a value as a parameter adds that value to the end of the list. The set replaces the value at that index with the new value. The add with parameters of an index and a value puts the passed value at that index and moves any existing values by one index to the right (increments the index).
[1, 4, 2, 6, 3]
The add with an index of 2 and a value of 5 adds the 5 at index 2 not 1. Remember that the first index is 0.
[1, 2, 4, 6, 3]
When you declare and create a collection class you can specify the type of the items in it.
You can step through the code above by clicking on the link Example-8-12-4 4
What is printed as a result of executing the following code segment?
List<String> list1 = new ArrayList<String>();
list1.add("a");
list1.add("b");
list1.add(0,"c");
list1.add(1, "d");
list1.set(2, "e");
list1.add("f");
System.out.println(list1);
What is printed as a result of executing the following code segment?
[c, d, e, b]
What happened to the f?
[c, d, e, b, f]
This list is [a], then [a, b], then [c, a, b], then [c, d, a, b], then [c, d, e, b], then [c, d, e, b, f]
[c, a, e, b, f]
The a is pushed to position 2 and then replaced with the e.
[c, d, e, a, b, f]
This would be true if it was list1.add(2,"e")
[c, a, e, d, b, f]
Remember that the set will replace the value at index 2.
You can step through the code above by clicking on the link Example-8-12-6 6
What is printed as a result of executing the following code segment?
List<String> list1 = new ArrayList<String>();
list1.add("a");
list1.add("b");
list1.add(0,"c");
list1.set(1, "d");
list1.set(0, "e");
list1.add("b");
System.out.println(list1);
What is printed as a result of executing the following code segment?
[e, d, b]
This would be true if you couldn’t add a duplicate object to a list, but you can.
[e, d, b, b]
The list is [a], [a, b], [c, a, b], [c, d, b], [e, d, b], and then [e, d, b, b]
[e, d, a, b, b]
This would be true it list1.set(1,"d"); was list1.add(1,"d");
[e, d, a, b]
This would be true it list1.set(1,"d"); was list1.add(1,"d"); and if lists didn’t allow duplicate objects.
You can step through the code above by clicking on the following Example-8-12-8 8
Assume that numList has been initialized with the following Integer objects: [5, 7, 8, 12]. Which of the following shows the values in numList after a call to mystery(11)?
private List<Integer> numList;
public void mystery(int value)
{
int i = 0;
while (i < numList.size() && numList.get(i) < value)
{
i++;
}
numList.add(i, value);
}
[5, 7, 8, 12]
What about the 11?
[5, 7, 8, 11, 12]
This will add the value at the correct location in a list in ascending order.
[11, 5, 7, 8, 12]
This would be true if it was numList.add(0, value)
[5, 7, 8, 12, 11]
This would be true if the while loop was from 0 to one less than the size of the list.
[5, 7, 11, 8, 12]
This would be true if it was numList.add(i-1, value)
You can step through the code above by clicking on the following Example-8-12-10 10
Assume that nums has been created as an ArrayList object and initially contains the following Integer values: [0, 0, 4, 2, 5, 0, 3, 0]. What will nums contain as a result of executing the following method numQuest?
private List<Integer> nums;
//precondition: nums.size() > 0
//nums contains Integer objects
public void numQuest() {
int k = 0;
Integer zero = new Integer(0);
while (k < nums.size())
{
if (nums.get(k).equals(zero))
nums.remove(k);
k++;
}
}
[0, 0, 4, 2, 5, 0, 3, 0]
This shows the original values but this code does remove some zeros so this can’t be right.
[3, 5, 2, 4, 0, 0, 0, 0]
This shows all zeros at the end, but this code removes 0’s so this can’t be right.
[0, 0, 0, 0, 4, 2, 5, 3]
This shows all zeros at the beginning, but this code removes zeros so this can’t be right.
[4, 2, 5, 3]
This shows all zeros removed. This would be correct if k was only incremented if a value wasn’t removed.
[0, 4, 2, 5, 3]
This code will loop through the array list and if the current value at the current index (k) is 0, it will remove it. When you remove a value from an array list, it moves all values to the right of that down one. So the first 0 will be deleted but the second one will not since k is incremented even if you remove something. You should only increment k if you didn’t remove something and then you would remove all 0’s from the list.
You can step through the code above by clicking on the following Example-8-13-2 2