# Hard Multiple Choice Questions¶

These problems are about the same or harder than those you will see on the AP CS A exam.

8-13-1: What is in the list nums if it initially contained {5, 3, 1} and the following code is executed?

nums.add(6);
nums.remove(1);

• (A) [5, 3, 1, 6]
• The remove(1) removes the item at index 1 which will be 5 after the 4 is added at index 0.
• (B) [4, 3, 1, 6]
• The add(6) adds the 6 at the end of the list. The add(0,4) will add 4 at index 0. The remove(1) removes the 5 at index 1.
• (C) [4, 3, 6]
• The remove(1) doesn't remove the 1, it removes the value at index 1.
• (D) [5, 3, 6]
• The 5 will be removed with the remove(1).
• (E) [4, 5, 3, 6]
• This would be true if remove(1) removed the item with that value, but it removes the item at that index.

You can step through the code above by clicking on the following Example-8-13-1.

8-13-2: 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++;
}
}

• (A) [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.
• (B) [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.
• (C) [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.
• (D) [4, 2, 5, 3]
• This shows all zeros removed. This would be correct if k was only incremented if a value wasn't removed.
• (E) [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.

Next Section - Free Response - String Scramble B