Skip to main content
Logo image

Section 4.34 ArrayList Multiple-Choice Exercises

Section 4.34.1 ArrayList Easier Multiple Choice Questions

These problems are easier than most of those that you will usually see on the AP CSA exam.

Activity 4.34.1.

Which index is the last element in a list called nums at?
  • nums.length
  • You can’t use length on lists and the last index is one less than the size.
  • nums.length - 1
  • You can’t use length on lists, use size instead.
  • nums.size()
  • Since the first element in a list is at index 0 the last element is at the size minus 1.
  • nums.size() - 1
  • The last element is at the size of the list minus 1.

Activity 4.34.2.

Which of the following is a reason to use an array instead of an ArrayList?
  • An array has faster access to its elements than a list does.
  • Since an ArrayList is implemented by an array, it has the same access time.
  • An array knows it length, but a list doesn’t know its length.
  • Lists do know their length, but they don’t make it public.
  • An ArrayList can allocate more space than it needs.
  • Every time an ArrayList fills up a new array is created that is twice as big. This can lead to extra space that is wasted.

Activity 4.34.3.

Which of the following is a reason to use an ArrayList instead of an array?
  • An ArrayList can grow or shrink as needed, while an array is always the same size.
  • This is the main advantage to an ArrayList.
  • You can use a for-each loop on an ArrayList, but not in an array.
  • You can use a for-each loop on either an ArrayList or array.
  • You can store objects in an ArrayList, but not in an array.
  • Arrays can also store objects of the same type.

Activity 4.34.4.

Which of the following is the correct way to get the first value in a list called nums?
  • nums[0]
  • This is how you get the first value in an array, but not in a list.
  • nums[1]
  • This is how you get the second value in an array. Remember that this is a list and that the first item in an array is at index 0.
  • nums.first()
  • The List doesn’t have a first method.
  • nums.get(0)
  • Use the get method to get a value from a list and the first element in a list is at index 0.
  • nums.get(1)
  • This would return the second element in a list. Remember that the first element in a list or array is at index 0.

Activity 4.34.5.

Which of the following is the correct way to set the second value in a list called nums to 5?
  • nums[1] = 5;
  • This is how you set the second value in an array, but not in a list.
  • nums[2] = 5;
  • This is how you set the third value in an array, but not in a list.
  • nums.set(5, 1);
  • This would the value at index 5 to 1.
  • nums.set(1, 5);
  • This sets the second value in the list to 5.
  • nums.set(2, 5);
  • This would set the third value in the list to 5. Remember that the first value is at index 0.

Activity 4.34.6.

Which of the following is the correct way to remove the value 3 from the list nums = [5, 3, 2, 1]?
  • nums.remove(3);
  • This would remove the value at index 3 which is 1.
  • nums.remove(0);
  • This would remove the value at index 0 which is 5.
  • nums.remove(1);
  • This would remove the value at index 1 which is 3.
  • nums.remove(2);
  • This would remove the value at index 2 which is 2.

Activity 4.34.7.

Which of the following is the correct way to add 2 between the 1 and 3 in the following list nums = [1, 3, 4]?
  • nums.add(2, 0);
  • This would add 0 at index 2. Remember that the method is add(index, obj).
  • nums.add(2, 1);
  • This would add 1 at index 2. Remember that the method is add(index, obj)
  • nums.add(0, 2);
  • This would add 2 at index 0 which would result in [2, 1, 3, 4]
  • nums.add(1, 2);
  • This would add 2 at index 1 which would result in [1, 2, 3, 4]
  • nums.add(2, 2);
  • This would add 2 at index 2 which would result in [1, 3, 2, 4]

Activity 4.34.8.

What will print when the following code executes?
List<Integer> list1 = new ArrayList<Integer>();
list1.add(new Integer(1));
list1.add(new Integer(2));
list1.add(new Integer(3));
list1.remove(1);
System.out.println(list1);
  • [2, 3]
  • This would be true if it was remove(0)
  • [1, 2, 3]
  • The remove will remove a value from the list, so this can’t be correct.
  • [1, 2]
  • This would be true if it was remove(2)
  • [1, 3]
  • This removes the value at index 1 which is 2.
You can step through the code above by clicking on the following Ex-8-11-9
 1 
http://cscircles.cemc.uwaterloo.ca/java_visualize/#code=import+java.util.*%3B%0Apublic+class+Test+%7B%0A+++public+static+void+main(String%5B%5D+args)+%7B%0A++++++List%3CInteger%3E+list1+%3D+new+ArrayList%3CInteger%3E()%3B%0A++++++list1.add(new+Integer(1))%3B%0A++++++System.out.println(list1)%3B%0A++++++list1.add(new+Integer(2))%3B%0A++++++System.out.println(list1)%3B%0A++++++list1.add(new+Integer(3))%3B%0A++++++System.out.println(list1)%3B%0A++++++list1.remove(1)%3B%0A++++++System.out.println(list1)%3B%0A++++++%0A+++%7D%0A%7D&mode=display&curInstr=0
.

Activity 4.34.9.

What will print when the following code executes?
List<String> list1 = new ArrayList<String>();
list1.add("Anaya");
list1.add("Layla");
list1.add("Sharrie");
list1.set(0, "Destini");
list1.add(0, "Sarah");
System.out.println(list1);
  • ["Sarah", "Destini", "Layla", "Sharrie"]
  • The list is first ["Anaya", "Layla", "Sharrie"] and then ["Destini, "Layla", "Sharrie"] and finally ["Sarah", "Destini, "Layla", "Sharrie"]
  • ["Sarah", "Destini", "Anaya", "Layla", "Sharrie"]
  • The set replaces the value at index 0.
  • ["Sarah", "Layla", "Sharrie"]
  • This would be true if the second add was a set.
  • ["Destini", "Layla", "Sharrie", "Sarah"]
  • This would be true if the last add didn’t have an index of 0.
You can step through the code above by clicking on the following Ex-8-11-10
 2 
http://cscircles.cemc.uwaterloo.ca/java_visualize/#code=import+java.util.*%3B%0Apublic+class+Test+%7B%0A+++public+static+void+main(String%5B%5D+args)+%7B%0A++++++List%3CString%3E+list1+%3D+new+ArrayList%3CString%3E()%3B%0A++++++list1.add(%22Anaya%22)%3B%0A++++++System.out.println(list1)%3B%0A++++++list1.add(%22Layla%22)%3B%0A++++++System.out.println(list1)%3B%0A++++++list1.add(%22Sharrie%22)%3B%0A++++++System.out.println(list1)%3B%0A++++++list1.set(0,+%22Destini%22)%3B%0A++++++System.out.println(list1)%3B%0A++++++list1.add(0,+%22Sarah%22)%3B%0A++++++System.out.println(list1)%3B%0A++++++%0A+++%7D%0A%7D&mode=display&curInstr=0
.

Section 4.34.2 ArrayList Medium Multiple Choice Questions

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

Activity 4.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 
http://cscircles.cemc.uwaterloo.ca/java_visualize/#code=import+java.util.*%3B%0Apublic+class+Test+%7B%0A+++public+static+void+main(String%5B%5D+args)+%7B%0A++++++List%3CInteger%3E+list1+%3D+new+ArrayList%3CInteger%3E()%3B%0A++++++list1.add(new+Integer(1))%3B%0A++++++System.out.println(list1)%3B%0A++++++list1.add(new+Integer(2))%3B%0A++++++System.out.println(list1)%3B%0A++++++list1.add(new+Integer(3))%3B%0A++++++System.out.println(list1)%3B%0A++++++list1.set(2,+new+Integer(4))%3B%0A++++++System.out.println(list1)%3B%0A++++++list1.add(2,+new+Integer(5))%3B%0A++++++System.out.println(list1)%3B%0A++++++list1.add(new+Integer(6))%3B%0A++++++System.out.println(list1)%3B%0A+++%7D%0A%7D&mode=display&curInstr=0
.

Activity 4.34.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 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 
http://cscircles.cemc.uwaterloo.ca/java_visualize/#code=import+java.util.*%3B%0Apublic+class+ListWorker+%7B%0A+++%0A+++private+List%3CInteger%3E+nums%3B%0A+++%0A+++public+ListWorker(List%3CInteger%3E+theNums)%0A+++%7B%0A++++++nums+%3D+theNums%3B%0A+++%7D%0A%0A+++//+precondition%3A+nums.size()+%3E+0%3B%0A+++//+nums+contains+Integer+objects%0A+++public+void+numQuest()%0A+++%7B%0A+++++++int+k+%3D+0%3B%0A+++++++Integer+zero+%3D+new+Integer(0)%3B%0A+++++++while+(k+%3C+nums.size())%0A+++++++%7B%0A+++++++++System.out.println(%22List%3A+%22+%2B+nums+%2B+%22+and+k+is+%22+%2B+k)%3B%0A+++++++++if+(nums.get(k).equals(zero))%0A+++++++++++nums.remove(k)%3B%0A+++++++++else%0A+++++++++++k%2B%2B%3B%0A+++++++%7D%0A+++%7D%0A+++%0A+++public+static+void+main(String%5B%5D+args)%0A+++%7B%0A++++++List%3CInteger%3E+myList+%3D+new+ArrayList%3CInteger%3E()%3B%0A++++++myList.add(0)%3B%0A++++++myList.add(0)%3B%0A++++++myList.add(4)%3B%0A++++++myList.add(2)%3B%0A++++++myList.add(5)%3B%0A++++++myList.add(0)%3B%0A++++++myList.add(3)%3B%0A++++++ListWorker+lWorker+%3D+new+ListWorker(myList)%3B%0A++++++lWorker.numQuest()%3B%0A++++++System.out.println(myList)%3B+%0A++++++%0A+++%7D%0A+++%0A%7D&mode=display&curInstr=0
.

Activity 4.34.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++)
      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 
http://cscircles.cemc.uwaterloo.ca/java_visualize/#code=import+java.util.*%3B%0Apublic+class+Test+%7B%0A+++%0A++++++public+static+List%3CInteger%3E+process1(int+n)%0A++++++%7B%0A+++++++++List%3CInteger%3E+someList+%3D+new+ArrayList%3CInteger%3E()%3B%0A+++++++++for+(int+k+%3D+0%3B+k+%3C+n%3B+k%2B%2B)%0A+++++++++%7B%0A++++++++++++someList.add(k)%3B%0A++++++++++++System.out.println(someList)%3B%0A+++++++++%7D%0A+++++++++return+someList%3B%0A++++++%7D%0A++++++%0A++++++public+static+List%3CInteger%3E+process2(int+n)%0A++++++%7B%0A+++++++++List%3CInteger%3E+someList+%3D+new+ArrayList%3CInteger%3E()%3B%0A+++++++++for+(int+k+%3D+0%3B+k+%3C+n%3B+k%2B%2B)%0A+++++++++%7B%0A++++++++++++someList.add(k,+k)%3B%0A++++++++++++System.out.println(someList)%3B%0A+++++++++%7D%0A+++++++++return+someList%3B%0A++++++%7D%0A+++%0A+++public+static+void+main(String%5B%5D+args)+%7B%0A++++++List%3CInteger%3E+myList+%3D+process1(5)%3B%0A++++++List%3CInteger%3E+myList2+%3D+process2(5)%3B%0A+++++%0A+++%7D%0A%7D&mode=display&curInstr=0
.

Activity 4.34.4.

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 
http://cscircles.cemc.uwaterloo.ca/java_visualize/#code=import+java.util.*%3B%0Apublic+class+Test+%7B%0A+++public+static+void+main(String%5B%5D+args)+%7B%0A+++++List%3CInteger%3E+aList+%3D+new+ArrayList%3CInteger%3E()%3B%0A+++++aList.add(new+Integer(1))%3B+%0A+++++System.out.println(aList)%3B%0A+++++aList.add(new+Integer(2))%3B+%0A+++++System.out.println(aList)%3B%0A+++++aList.add(1,+new+Integer(5))%3B+%0A+++++System.out.println(aList)%3B%0A+++++aList.set(1,+new+Integer(4))%3B%0A+++++System.out.println(aList)%3B%0A+++++aList.add(new+Integer(6))%3B+%0A+++++System.out.println(aList)%3B%0A+++++aList.add(new+Integer(3))%3B%0A+++++System.out.println(aList)%3B%0A+++%7D%0A%7D&mode=display&curInstr=0
.

Activity 4.34.5.

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.remove(1);
aList.add(1, new Integer(3));
aList.set(1, new Integer(4));
aList.add(new Integer(5));
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
 5 
http://cscircles.cemc.uwaterloo.ca/java_visualize/#code=import+java.util.*%3B%0Apublic+class+Test+%7B%0A+++public+static+void+main(String%5B%5D+args)+%7B%0A+++++List%3CInteger%3E+aList+%3D+new+ArrayList%3CInteger%3E()%3B%0A+++++aList.add(new+Integer(1))%3B+%0A+++++System.out.println(aList)%3B+%0A+++++aList.add(new+Integer(2))%3B+%0A+++++System.out.println(aList)%3B+%0A+++++aList.remove(1)%3B%0A+++++System.out.println(aList)%3B+%0A+++++aList.add(1,+new+Integer(3))%3B+%0A+++++System.out.println(aList)%3B+%0A+++++aList.set(1,+new+Integer(4))%3B%0A+++++System.out.println(aList)%3B+%0A+++++aList.add(new+Integer(5))%3B+%0A+++++System.out.println(aList)%3B+%0A+++%7D%0A%7D&mode=display&curInstr=0
.

Activity 4.34.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.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 
http://cscircles.cemc.uwaterloo.ca/java_visualize/#code=import+java.util.*%3B%0Apublic+class+Test+%7B%0A+++public+static+void+main(String%5B%5D+args)+%7B%0A+++++List%3CString%3E+list1+%3D+new+ArrayList%3CString%3E()%3B%0A+++++list1.add(%22a%22)%3B%0A+++++System.out.println(list1)%3B%0A+++++list1.add(%22b%22)%3B%0A+++++System.out.println(list1)%3B%0A+++++list1.add(0,%22c%22)%3B%0A+++++System.out.println(list1)%3B%0A+++++list1.add(1,+%22d%22)%3B%0A+++++System.out.println(list1)%3B%0A+++++list1.set(2,+%22e%22)%3B%0A+++++System.out.println(list1)%3B%0A+++++list1.add(%22f%22)%3B%0A+++++System.out.println(list1)%3B%0A+++%7D%0A%7D&mode=display&curInstr=0
.

Activity 4.34.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
 7 
http://cscircles.cemc.uwaterloo.ca/java_visualize/#code=import+java.util.*%3B%0Apublic+class+Test+%7B%0A+++public+static+void+main(String%5B%5D+args)+%7B%0A++++++List%3CInteger%3E+list1+%3D+new+ArrayList%3CInteger%3E()%3B%0A++++++list1.add(4)%3B%0A++++++list1.add(2)%3B%0A++++++list1.add(3)%3B%0A++++++list1.add(4)%3B%0A++++++list1.add(5)%3B%0A++++++System.out.println(list1)%3B%0A++++++list1.remove(4)%3B%0A++++++System.out.println(list1)%3B%0A+++%7D%0A%7D&mode=display&curInstr=0
.

Activity 4.34.8.

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 
http://cscircles.cemc.uwaterloo.ca/java_visualize/#code=import+java.util.*%3B%0Apublic+class+Test+%7B%0A+++public+static+void+main(String%5B%5D+args)+%7B%0A+++++List%3CString%3E+list1+%3D+new+ArrayList%3CString%3E()%3B%0A+++++list1.add(%22a%22)%3B%0A+++++System.out.println(list1)%3B%0A+++++list1.add(%22b%22)%3B%0A+++++System.out.println(list1)%3B%0A+++++list1.add(0,%22c%22)%3B%0A+++++System.out.println(list1)%3B%0A+++++list1.set(1,+%22d%22)%3B%0A+++++System.out.println(list1)%3B%0A+++++list1.set(0,+%22e%22)%3B%0A+++++System.out.println(list1)%3B%0A+++++list1.add(%22b%22)%3B%0A+++++System.out.println(list1)%3B%0A+++%7D%0A%7D&mode=display&curInstr=15
.

Activity 4.34.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);
        numList.add(obj);
    }
}
  • [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
 9 
http://cscircles.cemc.uwaterloo.ca/java_visualize/#code=import+java.util.*%3B%0A+++%0Apublic+class+ListTester%0A%7B%0A+++%0A++++private+List%3CInteger%3E+numList+%3D+null%3B%0A+++%0A++++public+ListTester(List%3CInteger%3E+myList)%0A++++%7B%0A+++++++numList+%3D+myList%3B%0A++++%7D%0A+++%0A++++public+void+mystery(int+n)%0A++++%7B%0A++++++++for+(int+i+%3D+0%3B+i+%3C+n%3B+i%2B%2B)%0A++++++++%7B%0A++++++++++++Integer+obj+%3D+numList.remove(0)%3B%0A++++++++++++numList.add(obj)%3B%0A++++++++%7D%0A++++%7D%0A++++++%0A++++public+static+void+main(String%5B%5D+args)+%0A++++%7B%0A+++++++List%3CInteger%3E+aList+%3D+new+ArrayList%3CInteger%3E()%3B%0A+++++++aList.add(0)%3B%0A+++++++aList.add(1)%3B%0A+++++++aList.add(2)%3B%0A+++++++aList.add(3)%3B%0A+++++++aList.add(4)%3B%0A+++++++ListTester+tester+%3D+new+ListTester(aList)%3B%0A+++++++System.out.println(tester.numList)%3B%0A+++++++tester.mystery(5)%3B%0A+++++++System.out.println(tester.numList)%3B%0A+++%0A++++%7D%0A%7D&mode=display&curInstr=0
.

Activity 4.34.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++;
    }
    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 
http://cscircles.cemc.uwaterloo.ca/java_visualize/#code=import+java.util.*%3B%0A+++%0Apublic+class+ListTester%0A%7B%0A+++%0A++++private+List%3CInteger%3E+numList+%3D+null%3B%0A+++%0A++++public+ListTester(List%3CInteger%3E+myList)%0A++++%7B%0A+++++++numList+%3D+myList%3B%0A++++%7D%0A+++%0A++++public+void+mystery(int+value)%0A++++%7B%0A++++++++int+i+%3D+0%3B%0A++++++++while+(i+%3C+numList.size()+%26%26+numList.get(i)+%3C+value)%0A++++++++%7B+%0A++++++++++++i%2B%2B%3B%0A++++++++%7D%0A++++++++numList.add(i,+value)%3B%0A++++%7D%0A++++++%0A++++public+static+void+main(String%5B%5D+args)+%0A++++%7B%0A+++++++List%3CInteger%3E+aList+%3D+new+ArrayList%3CInteger%3E()%3B%0A+++++++aList.add(5)%3B%0A+++++++aList.add(7)%3B%0A+++++++aList.add(8)%3B%0A+++++++aList.add(12)%3B%0A+++++++ListTester+tester+%3D+new+ListTester(aList)%3B%0A+++++++System.out.println(tester.numList)%3B%0A+++++++tester.mystery(11)%3B%0A+++++++System.out.println(tester.numList)%3B%0A+++%0A++++%7D%0A%7D&mode=display&curInstr=0
.

Section 4.34.3 ArrayList Hard Multiple Choice Questions

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

Activity 4.34.1.

What is in the list nums if it initially contained {5, 3, 1} and the following code is executed?
nums.add(6);
nums.add(0,4);
nums.remove(1);
  • [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.
  • [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.
  • [4, 3, 6]
  • The remove(1) doesn’t remove the 1, it removes the value at index 1.
  • [5, 3, 6]
  • The 5 will be removed with the remove(1).
  • [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
 1 
http://cscircles.cemc.uwaterloo.ca/java_visualize/#code=import+java.util.*%3B%0Apublic+class+Test+%7B%0A+++public+static+void+main(String%5B%5D+args)+%7B%0A++++++List%3CInteger%3E+nums+%3D+new+ArrayList%3CInteger%3E()%3B%0A++++++nums.add(5)%3B%0A++++++System.out.println(nums)%3B%0A++++++nums.add(3)%3B%0A++++++System.out.println(nums)%3B%0A++++++nums.add(1)%3B%0A++++++System.out.println(nums)%3B%0A++++++nums.add(6)%3B%0A++++++System.out.println(nums)%3B%0A++++++nums.add(0,4)%3B%0A++++++System.out.println(nums)%3B%0A++++++nums.remove(1)%3B%0A++++++System.out.println(nums)%3B%0A+++%7D%0A%7D&mode=display&curInstr=0
.

Activity 4.34.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++;
   }
}
  • [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 
http://cscircles.cemc.uwaterloo.ca/java_visualize/#code=import+java.util.*%3B%0Apublic+class+ListWorker+%7B%0A+++%0A+++private+List%3CInteger%3E+nums%3B%0A+++%0A+++public+ListWorker(List%3CInteger%3E+theNums)%0A+++%7B%0A++++++nums+%3D+theNums%3B%0A+++%7D%0A%0A+++//+precondition%3A+nums.size()+%3E+0%3B%0A+++//+nums+contains+Integer+objects%0A+++public+void+numQuest()%0A+++%7B%0A+++++++int+k+%3D+0%3B%0A+++++++Integer+zero+%3D+new+Integer(0)%3B%0A+++++++while+(k+%3C+nums.size())%0A+++++++%7B%0A+++++++++System.out.println(%22List%3A+%22+%2B+nums+%2B+%22+and+k+is+%22+%2B+k)%3B%0A+++++++++if+(nums.get(k).equals(zero))%0A+++++++++++nums.remove(k)%3B%0A+++++++++k%2B%2B%3B%0A+++++++%7D%0A+++%7D%0A+++%0A+++public+static+void+main(String%5B%5D+args)%0A+++%7B%0A++++++List%3CInteger%3E+myList+%3D+new+ArrayList%3CInteger%3E()%3B%0A++++++myList.add(0)%3B%0A++++++myList.add(0)%3B%0A++++++myList.add(4)%3B%0A++++++myList.add(2)%3B%0A++++++myList.add(5)%3B%0A++++++myList.add(0)%3B%0A++++++myList.add(3)%3B%0A++++++ListWorker+lWorker+%3D+new+ListWorker(myList)%3B%0A++++++lWorker.numQuest()%3B%0A++++++System.out.println(myList)%3B+%0A++++++%0A+++%7D%0A+++%0A%7D&mode=display&curInstr=0
.
For more practice with free response questions with Lists, look up the following FRQs in this FRQs
 3 
https://docs.google.com/spreadsheets/d/1Q0pbL9qawN8XlUctkDIiqsP6XdwR-IcWZ_cwauHy0-U/edit#gid=1826848698
spreadsheet:
You have attempted of activities on this page.