Medium Multiple Choice QuestionsΒΆ

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

8-8-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);





8-8-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);
   k++;
  }
}






8-8-3: Which of the following best describes the behavior of process1 and process2 (shown below)?

public List process1(int n)
{
   List<Integer> someList = new ArrayList<Integer>();
   for (int k = 0; k < n; k++)
      someList.add(k);
   return someList;
}

public List process2(int n)
{
   List<Integer> someList = new ArrayList<Integer>();
   for (int k = 0; k < n; k++)
      someList.add(k, k);
   return someList;
}






8-8-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(list);