# 9.19. Medium Multiple Choice Questions¶

These problems are like those you will see on the AP CS A exam.

8-12-1: What is printed as a result of executing the following code segment?

List<Integer> list1 = new ArrayList<Integer>();
list1.set(2, new Integer(4));
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.

8-12-2: 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 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.

8-12-3: 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++)
return someList;
}

public static List<Integer> process2(int n)
{
List<Integer> someList = new ArrayList<Integer>();
for (int k = 0; k < n; 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.

8-12-4: What is printed as a result of executing the following code segment?

List<Integer> aList = new ArrayList<Integer>();
aList.set(1, new Integer(4));
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.

8-12-5: What is printed as a result of executing the following code segment?

List<Integer> aList = new ArrayList<Integer>();
aList.remove(1);
aList.set(1, new Integer(4));
System.out.println(list);

• [1, 2, 3, 4, 5]
• This would be true if the code just added each integer at the end of the list. But, that is not what it does.
• [1, 4, 5]
• The list is [1], then [1, 2], then [1], then [1, 3], then [1, 4], then [1, 4, 5].
• [1, 4, 3, 5]
• This would be true if the set was an add.
• [2, 4, 5]
• This would be true it it was remove(0). Remember that it removes the object at the given index.
• [2, 4, 3, 5]
• This would be true if the set was an add and if it was remove(0).

You can step through the code above by clicking on the link Example-8-12-5.

8-12-6: What is printed as a result of executing the following code segment?

List<String> list1 = new ArrayList<String>();
list1.set(2, "e");
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.

8-12-7: Given the list nums = [4, 2, 3, 4, 5] what is the result after executing nums.remove(4)?
• [2, 3, 4, 5]
• This would be true if it removed the first 4 but it removes the value at index 4.
• [2, 3, 5]
• This would be true if it removed all the 4 values, but it removes the value at index 4.
• [4, 2, 3, 5]
• This would be true if it removed the value at index 3.
• [4, 2, 3, 4]
• This removes the value at index 4 which is 5.

You can step through the code above by clicking on the following Example-8-12-7.

8-12-8: What is printed as a result of executing the following code segment?

List<String> list1 = new ArrayList<String>();
list1.set(1, "d");
list1.set(0, "e");
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-12-9: Assume that numList has been initialized with the following Integer objects: [0, 1, 2, 3, 4]. What is the value of numList after mystery(5) executes?

private List<Integer> numList;
public void mystery(int n)
{
for (int i = 0; i < n; i++)
{
Integer obj = numList.remove(0);
}
}

• [4, 3, 2, 1, 0]
• This would be true if it was numList.add(numList.size() - i, obj)
• [1, 2, 3, 4, 0]
• This would be true if it was mystery(1)
• [0, 1, 2, 3, 4]
• Each value is removed one at a time and added to the end of the list which results in the same list.
• [2, 3, 4, 0, 1]
• This would be true if it was mystery(2)
• [4, 0, 1, 2, 3]
• This would be true if it was mystery(4)

You can step through the code above by clicking on the following Example-8-12-9.

8-12-10: 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++;
}
}

• [5, 7, 8, 12]
• This would be true if it was numList.add(0, value)
• This would be true if it was numList.add(i-1, value)