Hard Multiple Choice QuestionsΒΆ

These problems are harder than most of those that you will usually see on the AP CS A exam.

6-8-1: Assume that nums has been created as an ArrayList object and initially contains the following Integer values: [o, 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++;
   }
}






6-8-2: Which best characterizes the running time of the following code segment?

for (int j = 1; j <= n; j++) {
   for (int k = 1; k <= n; k = k * 2)
      System.out.println(j + " " + k);
}