Medium Multiple Choice QuestionsΒΆ

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

7-10-1: Which of the following statements is a valid conclusion. Assume that variable b is an array of k integers and that the following is true:

b[0] != b[k] for all k such that 1 <= k






7-10-2: Consider the following code segment. Which of the following statements best describes the conditions need for temp = true?

boolean temp = false;
for (int i = 0; i < a.length; i++) {
   temp = (a[i] == val);
}
return temp;






7-10-3: Consider the following data field and method findLongest. Mehtod findLongest is intended to find the longest consecutive block of the value target occurring in the array nums; however, finLongest does not work as intended. For example, if the array nums contains the values [7, 10, 10, 15, 15, 15, 15, 10, 10, 10, 15, 10, 10], the call findLongest(10) should return 3, the loength of the longest consecutive block of 10s. Which of the following best describes the value return by a call to findLongest?

private int[] nums;
public int findLongest(int target) {
   int lenCount = 0;
   int maxLen = 0;
   for (int k = 0; k < nums.length; k++) {
      if (nums[k] == target) {
         lenCount++;
      } else if (lenCount > maxLen) {
         maxLen = lenCount;
      }
   }
   if (lenCount > maxLen) {
      maxLen = lenCount;
   }






7-10-4: Consider the following data field and method. Which of the following best describes the contents of myStuff after the following statement has been executed?

private int[] myStuff;

//precondition: myStuff contains
//   integers in no particular order
public int mystery(int num) {
   for (int k = myStuff.length - 1; k >= 0; k--) {
      if (myStuff[k] < num) {
          return k;
      }
   }
   return -1;
}



int m = mystery(n)