You are on page 1of 13

1

Unit 10 Exam – Alternative Version – Solutions


1. Which of the following is not a step in the Binary-Search algorithm?
a. Find the value at the midpoint of the array ➜ Incorrect. This is a step in the binary-search algorithm
b. Compare the target value to the midpoint value ➜ Incorrect. This is a step in the binary-search algorithm
c. Repeat until we find the target value or low ➜ The ending condition should be when low > high
< high otherwise the loop will stop immediately.
d. If the target value is less than the midpoint ➜ Incorrect. This is a step in the binary-search algorithm
value set high = mid - 1 ➜ Incorrect. This is not true.
e. All of the above are steps of the binary search
algorithm

2. When you pass a double variable to a method, the method receives ______.
a. a copy of the variable ➜ double is a primitive data type. And when a primitive data type
variable is passed into a method as a parameter, a copy of it is made.
So any changes made to the variable within the method are made to
the copy and do NOT affect the original.
b. a copy of the memory address ➜ The value is copied.
c. the reference of the variable ➜ Primitive types do not use references.
d. the length of the variable ➜ The value is copied.
e. binary of the memory address ➜ The value is copied.

3. Consider the following method.

public static void recurMethod(String str)


{
if (str.length() > 2)
{
recurMethod(str.substring(1, str.length() - 1));
}
System.out.println(str);
}

Which of the following is printed as a result of the call recurMethod("Java")?


a. a ➜ This is incorrect. The initial call to recurMethod prints the argument passed: “Java”.
av
ava
avaJ

b. Ja ➜ This is incorrect. The initial call to recurMethod prints the argument passed: “Java”.
av
va

c. J ➜ This is incorrect. The initial call to recurMethod prints the argument passed: “Java”.
a
v
a

Unit 10 • Alternative Exam AP Computer Science A


2

d. J ➜ This is incorrect. Each subsequent call of recurMethod removes the first and last letters,
Ja the first print out cannot be just the letter “J”
Jav
Java

e. av ➜ This is correct. Each time recurMethod is called, it calls itself with the same string
Java parameter with the first and last character removed until the length is smaller than 3.
After this the method stops calling itself and moves on to the last line which prints
the string parameter. This completes with the final call of the method first (with
parameter “av”), then continues with each previous call until the original (with
parameter “Java”) is complete.

4. Consider the following recursive method.

public static void wackyPrinter(String str)


{
if (str.length() < 12)
{
wackyPrinter("!" + str + "!");
}
else
{
System.out.println(str);
}
}

What will be printed as a result of the call wackyPrinter("computer")?


a. computer ➜ This is incorrect. When wackyPrinter is initially called, the
parameter str (set to computer) does not meet the condition
str.length() < 12, so will not be printed. Instead the method is called
again with a different string.
b. !!computer! ➜ This is incorrect. Each time wackyPrinter calls itself it adds an “!”
both before and after the string from the previous call. As the
parameter in the initial call does not contain “!”, the number of times
“!” appears at the start and end of the string that is eventually printed
will be the same.
c. !!computer!! ➜ This is correct. The method checks whether the length of str
is smaller than 12. If it is, then the method is called again with a
new parameter which is formed by joining a “!” both before and
after the string from the previous call. This continues until the
parameter is no longer smaller than 12, at which point it is
printed. The parameters passed to the method in order are
therefore computer, !computer!, !!computer!! and only the last
of these has a length smaller than 12 and is printed as a result.
d. computer computer! computer!! ➜ This is incorrect. The method only prints the parameter str if it
has length not smaller than 12. Otherwise it calls itself with a new
parameter and does not print the old one.
e. computer !computer! !!computer!! ➜ This is incorrect. The method only prints the parameter str if it
has length not smaller than 12. Otherwise it calls itself with a new
parameter and does not print the old one.

Unit 10 • Alternative Exam AP Computer Science A


3

5. Which method(s) would produce the following output if they were passed the parameter, "gerbil"?

g
ge
ger
gerb
gerbi
gerbil

I. public static void mystery(String wo)


{
System.out.println(wo);
if (wo.length() > 1)
{
mystery(wo.substring(0, wo.length() - 1));
}
}

II. public static void mystery(String wo)


{
if (wo.length() > 1)
{
mystery(wo.substring(0, wo.length() - 1));
}
System.out.println(wo);
}

III. public static void mystery(String wo)


{
if (wo.length() > 1)
{
mystery(wo.substring( wo.length() - 1));
}
System.out.println(wo);
}

a. I only ➜ In choice I the print statement is before the recursive call so the prints would be in the opposite
order.
b. II only ➜ Choice II works because the method prints the string after the recursive call so the words
are printed out starting with the last call which is the string “g” because we work backwards
through the word.
c. III only ➜ In choice III the recursive call is just a substring of the last character of the string.
d. I and III only ➜ In choice I the print statement is before the recursive call so the prints would be in the opposite
order. In choice III the recursive call is just a substring of the last character of the string.
e. I, II and III ➜ In choice I the print statement is before the recursive call so the prints would be in the opposite
order. In choice III the recursive call is just a substring of the last character of the string.

Unit 10 • Alternative Exam AP Computer Science A


4

6. You need to search an ordered list of 1500 students for the student id #1124. Which search would be most
effective?

a. binary ➜ Correct. A binary search works on ordered lists, and will be much faster on a
large data set than linear search.
b. insertion ➜ Not a search algorithm.
c. linear ➜ Linear search will be much slower than a binary search, and does not take advantage
of the students being in order.
d. merge ➜ Not a search algorithm.
e. selection ➜ Not a search algorithm.

7. Consider the following code:

public static int mystery(int[] array, int target, int low, int high)
int mid = (low + high)/2;
if (target == array[mid])
{
return mid;
}
if (high < low)
{
return -1; // not found
}
if (target < array[mid])
{
return mystery(array, target, low, mid - 1);
}
if (target > array[mid])
{
return mystery(array, target, mid + 1, high);
}
return -1;
}

What algorithm is shown?


a. Binary Search ➜ Correct, the algorithm searches at a midpoint each time, and recursively moves
high and low to find the target value.
b. Insertion Sort ➜ Incorrect, this algorithm does not change the contents of the array so it cannot be a sort
algorithm.
c. Merge Sort ➜ See explanation for choice b.
d. Selection Sort ➜ See explanation for choice b.
e. Linear Search ➜ Incorrect, the elements in the array are NOT iterated through in order and checked to see
if they are equal to target.

Unit 10 • Alternative Exam AP Computer Science A


5

8. Consider the following method.

public static int goRound(int num)


{
if (num > 100)
{
return 10 * ((num + 50) / 10);
}
return 10 * goRound(num * 10);
}

What is returned as a result of the call goRound(5)?

a. 55555 ➜ This is incorrect. When the value num/10 is calculated it is done so using integer division. This
means the value of the last digit is lost each time a new call to goRound is made
b. 50000 ➜ This is incorrect. The final call goRound(500) has a return of 10*(500+50)/10 = 10*550/10 =
10*55 = 550, not 500.
c. 60000 ➜ This is incorrect. The final call goRound(500) has a return of 10*(500+50)/10 = 10*550/10 =
10*55 = 550, not 600.
d. 55000 ➜ This is correct. The initial call is goRound(5). This has a return of 10*goRound(50) = 10
* 10 * goRound (500). The call goRound(500) has a return of 10*(500+50)/10 = 550.
Therefore the value returned by the initial call is 10*10*550 = 55000.
e. 56000 ➜ This is incorrect. The final call goRound(500) has a return of 10*(500+50)/10 = 10*550/10 =
10*55 = 550, not 560.

9. Consider the following recursive method:

public static int recur(int x)


{
if (x >= 0)
{
return x - recur(x - 1);
}
return 0;
}

What is returned by the method call recur(9)?


a. 0 ➜ Incorrect. This method builds an arithmetic expression that evaluates to 5.
b. 5 ➜ This method subtracts from x with the value of the method called with a value one
smaller until x is 0. This means the method subtracts all the ints from the initial value of x
to 0 and 9 - 8 - 7 - 6 - 5 - 4 - 3 - 2 - 1 - 0 = 5.
9 - (8 - ( 7 - (6 - (5 - (4 - (3 - (2 - (1 - 0))))))))
9 - (8 - ( 7 - (6 - (5 - (4 - (3 - (2 - 1)))))))
9 - (8 - ( 7 - (6 - (5 - (4 - (3 - 1))))))
9 - (8 - ( 7 - (6 - (5 - (4 - 2)))))
9 - (8 - ( 7 - (6 - (5 - 2))))

Unit 10 • Alternative Exam AP Computer Science A


6

9 - (8 - ( 7 - (6 - 3)))
9 - (8 - ( 7 - 3))
9 - (8 - 4)
9-4=5
c. 9 ➜ Incorrect. This method builds an arithmetic expression that evaluates to 5.
d. -27 ➜ Incorrect. This method builds an arithmetic expression that evaluates to 5. The recursion
unwinds from the right hand side.
e. 45 ➜ This method does NOT sum all the ints from the initial value of x to 0.

Questions 10-11 refer to the following information.


Consider the following instance variable and methods. You may assume that word has been initialized as a String
with length greater than 0. The methods are intended to return true if a character appears twice in the same word;
false otherwise.

private String word;

public boolean hasRepeat()


{
return hasRepeatHelper(word.length() - 1);
}

private boolean hasRepeatHelper(int pos)


{
if (pos < 1)
{
return false;
}

String letter1 = word.substring(pos,pos+1);


for (int i = pos-1; i > 0; i--)
{
String letter2 = word.substring(i, i+1);

if (letter1.equals(letter2))
{
return true;
}
}
return hasRepeatHelper(pos-1);
}

10. The algorithm above has a mistake. Which of the calls to hasRepeat below will demonstrate the mistake?

I. “popular”
II. “mississippi”
III. “program”
a. I only ➜ This is correct. The error is in the for loop which doesn’t check the first letter
against other letters. In the word “popular” the algorithm will return false which is
incorrect.
b. II only ➜ This is incorrect. The word “mississippi” will return true which is correct in this case.
c. III only ➜ This is incorrect. The word “program” will return true which is correct in this case.

Unit 10 • Alternative Exam AP Computer Science A


7

d. II and III only ➜ This is incorrect. The word “program” will return true which is correct in this case. The
word “mississippi” will return true which is correct in this case.
e. I, II and III ➜ This is incorrect. The word “program” will return true which is correct in this case. The
word “mississippi” will return true which is correct in this case.

11. Which of the following should be used to replace the for loop header for (int i = pos-1; i > 0; i--) so
that hasRepeat will work as intended?
a. for (int i = pos-1; i >= 0; i--) ➜ This is correct. Changing the stopping condition to i >= 0
will ensure that all letters are checked against each other.
b. for (int i = pos; i > 0; i--) ➜ This is incorrect. This will check the same letter against itself
and thus always return true.
c. for (int i = pos-1; i > 0; i++) ➜ This is incorrect. This would result in an infinite loop.
d. for (int i = pos-1; i < 0; i--) ➜ This is incorrect. This would cause the loop to never start since
the condition of i < 0 would be false from the beginning.
e. for (int i = pos+1; i >= 0; i++) ➜ This is incorrect. This would cause an out of bounds error by
pushing i past the end of the word right from the beginning.

12. Which of the following are NOT real advantages of using the merge-sort algorithm?

I. It is easy to code
II. It is slow for large data sets
III. It uses no memory if coded correctly

a. I only ➜ The merge-sort algorithm is fast on large data sets and all algorithms use memory.
b. II only ➜ The merge-sort algorithm is quite challenging to code and all algorithms use memory.
c. I and II only ➜ All algorithms require memory use when they are implemented in code.
d. II and III only ➜ The merge-sort algorithm is quite challenging to code
e. I, II and III ➜ The merge-sort algorithm is quite challenging to code, but is fast when used on
large data sets. All algorithms require memory use when they are implemented in
code.

13. Consider the following recursive method.

public static String recur(int val)


{
if (val == 0)
{
return "";
}

if (val % 2 == 0)

{
return recur(val / 2) + "a";
}

Unit 10 • Alternative Exam AP Computer Science A


8

else
{
return recur(val / 2) + "b";
}
}

What is printed as a result of executing the statement System.out.println(recur(9))?


a. baab ➜ This is correct. The first call, recur(9) returns the value recur(4) + “b”.
By evaluating the call recur(4) we see that recur(9) is equivalent to recur(2)
+ “a” + “b”. Subsequent evaluations of the calls give this as equivalent to
recur(1) + “a” + “a” + “b”, which is equivalent to recur(0) + “b” + “a” + “a”
+ “b” which is equivalent to “” + “b” + “a” + “a” + “b”. Therefore the final
string returned is baab.
b. abba ➜ This is incorrect. Whenever a recursive call is made to this method, the result
of the call is concatenated onto the start of a single-character string. Therefore
the characters concatenated from the later calls will be at the start of the string
returned, with the characters from the first calls at the end.
c. ab ➜ This is incorrect. Whenever a recursive call is made to this method, the result
of the call is concatenated onto the start of a single-character string. Therefore
the final value of this call is more than just two characters, because we will have
four recursive calls in total.
d. aabba ➜ This is incorrect. Whenever a recursive call is made to this method, the result
of the call is concatenated onto the start of a single-character string. Therefore
the characters concatenated from the later calls will be at the start of the string
returned, with the characters from the first calls at the end.
e. Nothing is printed due ➜ This is incorrect. As each subsequent call to the method is with the previous
to infinite recursion argument divided by 2, eventually the method will be called with a parameter of
0, and the first return statement in the method of the string “” will be executed.

14. Consider the following recursive method.

/** Precondition: num > 0 */


public static int mystery(int num)
{
if (num % 2 == 1)
{
return 0;
}
else
{
return 1 + mystery(num / 2);
}
}

Unit 10 • Alternative Exam AP Computer Science A


9

Assume that int x has been declared and initialized with a value that satisfies the precondition of the method.
Which of the following best describes the value returned by the call mystery(x)?

a. The largest multiple of 2 which ➜ This is incorrect. The value returned is created only by adding 1. If x were for
divides x evenly example 6, then 6 divides it evenly but would not be returned by the method.
b. The exponent of the largest ➜ This is correct. The effect of this method is to call itself with a
power of 2 which divides x parameter which is half its value if it can be divided by 2. The result from
evenly this call is added to 1. In total, including the original call, the method is
called one more time than the number of times the original parameter can
be divided by 2 evenly. The final call returns a value of 0, and each return
is then added to 1. This means the result is the number of times we could
divide the num by 2 until reaching an odd number.

c. Nothing is returned. A run-time ➜ This is incorrect. The number passed to the recursive method each time is
error occurs because of infinite half of the previous value. Halving a number continuously guarantees that
recursion. eventually an odd value will be reached, at which point no more calls will be
made to the method.
d. The value 0 is returned ➜ This is incorrect. The call the method makes to itself is added to 1. The
method will therefore only return 0 if it is called exactly once - i.e. if x is odd.
e. The value x/2 is returned ➜ This is incorrect. This is the value that the method passes to itself. The
method will not simply return this value.

15. Consider the following code:

public static int mystery( int [] list, int val)


{
for (int i = 0; i<list.length; i++)
{
if (list[i] == val)
{
return i;
}
}
return -1;
}

What algorithm is this an implementation of?


a. Binary Search ➜ This algorithm does not find the midpoint of the array and break the problem down into
smaller halves.
b. Insertion Sort ➜ This algorithm does not change the contents of the array so it cannot be a sort algorithm.
c. Merge Sort ➜ This algorithm does not change the contents of the array so it cannot be a sort algorithm.
d. Selection Sort ➜ This algorithm does not change the contents of the array so it cannot be a sort algorithm.
e. Linear Search ➜ This is correct. The algorithm uses a for loop to iterate through every element of
the array list and checks to find the correct position of val.

16. Consider the following recursive method.

public static void doWhat(int num)


{
if (num < 12)
{

Unit 10 • Alternative Exam AP Computer Science A


10

System.out.print(num + " ");


doWhat(num + 3);
}
}

What is printed as a result of the call doWhat(2)?

a. 2 ➜ This is incorrect. The recursive call inside the if statement causes this code to continue running.
b. 11 ➜ This is incorrect. The print statement precedes the recursive call, so each call to doWhat will
print a different number.
c. 11 8 5 2 ➜ This is incorrect. The print statement precedes the recursive call, so each call to doWhat will
print before the next recursive call is made.

d. 2 5 8 11 ➜ This is correct. When the method doWhat is called, it checks if the condition num < 12 is
satisfied, where num is the parameter it was called with. If this is satisfied it then prints out
num and then calls doWhat again with a parameter which is 2 greater. On the final of these
calls num < 12 is no longer satisfied so that call completes. The printing happens before
the recursive call so the numbers will print out in the same order of the recursive calls.
e. 3 3 3 3 ➜ This is incorrect. The print statements are based on num which is being adjusted each time we
recursive call doWhat.

17. The two methods below are intended to implement the merge sort algorithm when used in conjunction.

/** Main method to sort an array of ints.


* @param arr the array to be sorted
* @return an array with the same contents as arr, sorted in
* increasing order
*/
public static int[] mergeSort(int[] arr)
{

// Base case: if list length is 1 then it is already sorted


if (arr.length <= 1)
{
return arr;
}

// Split list into two half-lists


/* missing code */
for (int i = 0; i < lowerHalf.length; i++)
{
lowerHalf[i] = arr[i];
}
for (int i = 0; i < upperHalf.length; i++)
{
upperHalf[i] = arr[i + arr.length / 2];
}

return merge(mergeSort(lowerHalf), mergeSort(upperHalf));


}

Unit 10 • Alternative Exam AP Computer Science A


11

/** Helper method which merges two ordered arrays of ints


* @param arr1 the first ordered array
* @param arr2 the second ordered array
* @return an array containing contents of both arr1 and arr2,
* sorted into increasing order
*/
private static int[] merge(int[] arr1, int[] arr2)
{
/* implementation not shown */
}

Which of the following calls should replace /* missing code */ in the code above for the merge sort to work
as intended?
a. int[] lowerHalf = new int[arr.length/2]; ➜ This is correct based on the way array indexing
int[] upperHalf = new int[(arr.length+1)/2]; works, the lower half should be size arr.length/2.
upperHalf should be (arr.length+1)/2 to
accommodate for odd length arrays
b. int[] lowerHalf = new int[arr.length]; ➜ Incorrect this will make arrays that are too large and
int[] upperHalf = new int[arr.length]; should be half the length of arr.

c. int[] lowerHalf = new int[arr.length-1]; ➜ Incorrect we do not need to subtract 1 from arr.length
int[] upperHalf = new int[arr.length+1]; since we should be dividing by 2.

d. int[] lowerHalf = new int[arr.length+1/2]; ➜ Incorrect we do not need to subtract 1 from arr.length
int[] upperHalf = new int[arr.length-1/2]; since we should be dividing by 2.

e. int[] lowerHalf = int[] upperHalf; ➜ Incorrect these are new arrays that need to be
declared and initialized.

18. Consider the following recursive method.

public int recur(int x)


{
if (x > 0)
{
return 2 * recur(x / 2);
}
if (x < 0)
{
return recur(x - 10) / 2;
}
return 10;
}

Unit 10 • Alternative Exam AP Computer Science A


12

What value is returned as a result of the call recur(5)?


a. 5 ➜ This is incorrect. Possible misconception of 5 - 10 = -5 happening during the
first call
b. -5 ➜ This is incorrect. X never drops below 0 so we never subtract 10.
c. -60 ➜ This is incorrect. X never drops below 0 so we never subtract 10.
d. 80 ➜ This is correct. The call recur(5) returns the value 2*recur(2). recur(2)
returns the value 2*recur(1). recur(1) returns the value 2*recur(0). recur(0)
returns 10. 10 * 2 * 2 * 2 = 80
e. Nothing is returned: an ➜ This is incorrect. The recursion stops when x is called with a value of 0
error is caused by infinite
recursion.

19. Consider the following recursive method.

public static void mystery(String s)


{
if (s.length() > 4)
{
mystery(s.substring(0, s.length() / 2));
mystery(s.substring(s.length() / 2));
}
else
{
System.out.println(s);
}
}

What is printed as a result of the call mystery("Mississippi")?


a. Mi ➜ This is incorrect. In the course of the original call, several calls are made to the
method with String arguments of length smaller than 4, so several lines are printed.
b. Mi ➜ This is incorrect. The two calls made after the initial call to mystery split the string
ss “Mississippi” into “Mi”, “ssi”, “ssi”, and “ppi”. These are not broken down any further as
is they are all under the length of 4.
ss
pp
i

c. Mis ➜ This is incorrect. The two calls made after the initial call to mystery split the string
sis “Mississippi” into “Mi” and “ssi” and they are never recombined so we would never see
sip a print out of “Mis”.
pi

d. Mi ➜ This is correct. The initial call, with s equal to “Mississippi” results in two
ssi calls to mystery, one with s equal to “Missi”, and one with s equal to “ssippi”.
ssi The first of these runs first and results in two further calls to mystery, with s
ppi equal to “Mi”, and s equal to “ssi”. Each of these strings is printed in the order
called, as both are under length 4. The call to mystery with s equal to “ssippi”
runs next: this creates another two calls to mystery, with s equal to “ssi”, and s
equal to “ppi”, and once again these are both printed in that order.

e. ippississiM ➜ This is incorrect. The string is broken into halves and not printed backwards
Unit 10 • Alternative Exam AP Computer Science A
13

20. Consider the following instance variable and method.

private ArrayList<Integer> vals;


// vals is sorted into increasing order

public void binaryInsert(int num)


{
int low = 0;
int high = vals.size();
while (high > low)
{
int mid = (low + high) / 2; /* calculate midpoint */
if (vals.get(mid).intValue() < num)
{
low = mid + 1;
}
else
{
high = mid;
}
}
vals.add(low, new Integer(num)); /* insert new number */
}

The binaryInsert method inserts an Integer element into the correct position of the list vals which is sorted into
increasing order. Suppose vals contains the following Integer values: [2, 3, 4, 5, 5, 8], and that the following
command is executed:

binaryInsert(5);

What will be the value of low when the line marked /* insert new number */ in the binaryInsert method is
executed?
a. 0 ➜ This is incorrect. This is the initial value of low, but this is changed during the course of the
method. The actual index returned places the new element in the correct place in the List (i.e. not
at the start.
b. 2 ➜ This is incorrect. On the third pass of the loop low is set to 3, having moved past the 2 position.
c. 3 ➜ This is correct. On the first pass of the while loop, the value of mid is set to 3. The
element at this index is not smaller than 5, so the value of high is set to mid, which is 3. The
second pass the value of mid is set to 1. The element at this index is smaller than 5, so the
value of low is set to mid + 1, which is 2. The third pass of the loop the value of mid is set to
2. The element at this index is smaller than 5, so the value of low is set to mid + 1, which is
3. At this point the values of high and low are the same, so the loop exits. The value of low
is at this point equal to 3, so this is the index at which the new element is inserted.
d. 4 ➜ This is incorrect. This would be a valid position for the new item to be placed, but is not the
value of low at this point.
e. 5 ➜ This is incorrect. On the first pass of the while loop the value of high is set to mid, which at the
time is 3. This is therefore the minimum value that could be returned by the procedure as the loop
would exit if at any point low was set to this value.

Unit 10 • Alternative Exam AP Computer Science A

You might also like