Name:     ID: 
 
Email: 

AP Computer Science Quiz 6

True/False
Indicate whether the statement is true or false.
 

 1. 

Before sending the compareTo message to an arbitrary object, that object must be cast to Comparable.
 

 2. 

When the element type of an array is a reference type or interface, objects of those types or any subtype (subclass or implementing class) can be directly inserted into the array.
 

 3. 

After accessing an object in an array, care must be taken to send it the appropriate messages or to cast it down to a type that can receive the appropriate messages.
 

 4. 

An ArrayList object’s physical size is automatically updated as needed.
 

 5. 

An ArrayList object can hold primitives.
 

 6. 

The algorithm to compute the factorial of a number cannot be expressed in a recursive algorithm.
 

 7. 

Consider the following method:

int recursiveMethod (int n){
   if (n == 1)
      return 1;
   else
      return n * recursiveMethod(n - 2);
}

This recursive method would always complete its recursion.
 

 8. 

O(n log n) complexity is generally better (more efficient) than O(n2).
 

 9. 

Consider an algorithm that is O(log n). Suppose you execute the algorithm once where the value of n is 10 and a second time where the value of n is 1000. The second execution will take three times longer than the first execution.
 

 10. 

The summation algorithm’s best, worst, and average cases are all O(n).
 

Multiple Choice
Identify the choice that best completes the statement or answers the question.
 

 11. 

The String class’s ____ method compares the contents of two String objects, but ignores the case of the characters.
a.
equalsCaseSensitive
c.
notEqualsCase
b.
equals
d.
equalsIgnoreCase
 

 12. 

The String class’s ____ method removes any white space from the beginning and end of a String object.
a.
removeSpace
c.
noWhiteSpace
b.
remove
d.
trim
 

 13. 

Which of the following will return the number of characters in a String object named s1?
a.
s1.length
c.
s1.length()
b.
s1.size
d.
s1.size()
 

 14. 

Consider the following code:

int search (int[] a, int searchValue){
   for (int i = 0; i < a.length; i++)
      if (a[i] == searchValue)
         return i;
   return -1;
}

This method performs a ____ search on an array.
a.
simplistic
c.
linear
b.
binary
d.
conditional
 

 15. 

Suppose an unsorted array of five integers contains the values {4, 2, 5, 1, 3}. If a selection sort is performed on this array, the values stored in the array after the second pass of the sorting algorithm would be ____.
a.
{4, 2, 5, 1, 3}
c.
{1, 2, 3, 5, 4}
b.
{1, 2, 3, 4, 5}
d.
{1, 2, 5, 4, 3}
 

 16. 

Consider the following pseudocode:

For each k from 1 to n - 1 (k is the index of array element to insert)
   Set itemToInsert to a[k]
   Set j to k - 1
   (j starts at k - 1 and is decremented until insertion   
   position is found)
      While (insertion position not found) and (not beginning
      of array)
         If itemToInsert < a[j]
            Move a[j] to index position j + 1
            Decrement j by 1
         Else
            The insertion position has been found
            itemToInsert should be positioned at index j + 1

This algorithm represents a(n) ____ sort.
a.
linear
c.
insertion
b.
selection
d.
bubble
 

 17. 

Which of the following algorithms is a correct recursive algorithm for summing n numbers?
a.
sum(n) = n + sum(n - 1) if n > 1
b.
sum(1) = 1
sum(n) = n + sum(n - 1) if n > 1
c.
sum(1) = 0
sum(n) = n + sum(n - 1) if n > 1
d.
sum(1) = 1
sum(n) = n + sum(n - 1) if n >= 1
 

 18. 

A recursive method with no stopping state will result in ____ recursion.
a.
definite
c.
infinite
b.
incidental
d.
finite
 

 19. 

Consider the following method:

int sum (int[] a){
   int result = 0;
   for (int i = 0; i < a.length; i++){
      result += a[i];
   }
   return result;
}

The complexity of the algorithm represented by the method is ____.
a.
O(1)
c.
O(n)
b.
O(log n)
d.
O(n log n)
 

 20. 

A linear search has a complexity of ____.
a.
O(log n)
c.
O(n log n)
b.
O(n)
d.
O(n2)
 

 21. 

Consider the following method:

int[] sumRows (int[][] a){
   int[] rowSum = new int[a.length];
   for (int row = 0; row < a.length; row++){
      for (int col = 0; col < a[row].length; col++){
         rowSum[row] += a[row][col];
      }
   }
   return rowSum;
}

The complexity of the algorithm represented by the method is ____.
a.
O(n)
c.
O(n2)
b.
O(n log n)
d.
O(n3)
 

 22. 

The complexity of a recursive algorithm for computing the nth Fibonacci is ____.
a.
O(n)
c.
O(n3)
b.
O(n2)
d.
O(rn)
 

 23. 

A thorough analysis of an algorithm’s complexity divides its behavior into ____ types of cases.
a.
2
c.
4
b.
3
d.
5
 

 24. 

Bubble sort’s best-case complexity is ____.
a.
O(1)
c.
O(n)
b.
O(log n)
d.
O(n2)
 

 25. 

The binary search algorithm is ____.
a.
O(log n)
c.
O(n log n)
b.
O(n)
d.
O(n2)
 



 
         Start Over