This book is now obsolete Please use CSAwesome instead.

13.8. Easy Multiple Choice QuestionsΒΆ

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

    13-8-1: What would the following code return from mystery([90, -30, 50], 50)?

     public static int mystery(int[] elements, int target)
     {
       for (int j = 0; j < elements.length; j++)
       {
          if (elements[j] == target)
          {
             return j;
          }
      }
      return -1;
    }
    
  • -1
  • This value is returned if the target is not in the list since this is a sequential search.
  • 0
  • This would be true if the target was 90 since this is a sequential search.
  • 1
  • This would be true if the target was -30 since this is a sequential search.
  • 2
  • This is a sequential search that returns the index where the target appears in the elements list
  • 50
  • A sequential search returns the index, not the value. What is the index of the 50?

You can step through the code above by clicking on the following Ex-12-7-1.

    13-8-2: What would the following code return from mystery([90, -30, 50], -20)?

     public static int mystery(int[] elements, int target)
     {
       for (int j = 0; j < elements.length; j++)
       {
          if (elements[j] == target)
          {
             return j;
          }
      }
      return -1;
    }
    
  • -1
  • A sequential search returns -1 if the target value is not found in the list.
  • 0
  • This would be true if the target was 90 since this is a sequential search.
  • 1
  • This would be true if the target was -30 since this is a sequential search.
  • 2
  • This would be true if the target was
  • -20
  • A sequential search returns negative one when the value isn't found in the list.

You can step through the code above by clicking on the following Ex-12-7-2.

    13-8-3: Consider the binarySearch method below. How many times would the while loop execute if you first do int[] arr = {2, 10, 23, 31, 55, 86} and then call binarySearch(arr,2)?

    public static int binarySearch(int[] elements, int target) {
       int left = 0;
       int right = elements.length - 1;
       while (left <= right)
       {
          int middle = (left + right) / 2;
          if (target < elements[middle])
          {
             right = middle - 1;
          }
          else if (target > elements[middle])
          {
             left = middle + 1;
          }
          else {
             return middle;
          }
        }
        return -1;
    }
    
  • 1
  • This would be true if we were looking for 23.
  • 2
  • It first compares 23 at index 2 (5 / 2 is 2) to 2. The second time it compares the 2 at index 0 (1 / 2 = 0) to 2 and returns 0.
  • 3
  • This would be true if we were looking for 10.

You can step through the code above by clicking on the following Ex-12-7-3.

    13-8-4: Which sort contains a recursive call?

  • selection sort
  • A selection sort has nested for loops.
  • insertion sort
  • An insertion sort has a while loop inside a for loop.
  • merge sort
  • A merge sort has a recursive call to mergeSortHelper in mergeSortHelper.

    13-8-5: Under what condition will an ascending insertion sort execute the slowest?

  • If the data is already sorted in ascending order
  • If the data is already sorted in the correct order you don't need to move any values.
  • If the data is already sorted in descending order
  • All values will have to be moved multiple times since the data was sorted into descending order.
  • It will always take the same amount of time to execute
  • This would be true if it was a selection sort.
You have attempted of activities on this page