chapter11: recursion multiple choice - cs215 -...

57
Chapter11: Recursion Multiple Choice 1. Which of the following statements is correct about a recursive function? A) A recursive function must call another function. B) A recursive function calls itself. C) A recursive function must be simple. D) A recursive function must never call another function. Ans: B Title: Which is correct about a recursive function? Difficulty: Easy Section Ref: 11.1 2. Which of the following is true about using recursion? A) A recursive computation solves a problem by calling itself with simpler input. B) A recursion eventually exhausts all available memory, causing the program to terminate. C) Recursion always helps you create a more efficient solution than other techniques. D) None of the listed options. Ans: A Title: Which is true about using recursion? Difficulty: Easy Section Ref: 11.1 3. How can you ensure that a recursive function terminates? A) Provide a special case for the most complex inputs. B) Provide a special case for the simplest inputs. C) Call the recursive function with simpler inputs. D) Use more than one return statement. Ans: B Title: How can you ensure that a recursive function terminates?

Upload: dangdat

Post on 15-May-2018

333 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

Chapter11: Recursion

Multiple Choice

1. Which of the following statements is correct about a recursive function?

A) A recursive function must call another function.

B) A recursive function calls itself.

C) A recursive function must be simple.

D) A recursive function must never call another function.

Ans: B

Title: Which is correct about a recursive function?

Difficulty: Easy

Section Ref: 11.1

2. Which of the following is true about using recursion?

A) A recursive computation solves a problem by calling itself with simpler input.

B) A recursion eventually exhausts all available memory, causing the program to terminate.

C) Recursion always helps you create a more efficient solution than other techniques.

D) None of the listed options.

Ans: A

Title: Which is true about using recursion?

Difficulty: Easy

Section Ref: 11.1

3. How can you ensure that a recursive function terminates?

A) Provide a special case for the most complex inputs.

B) Provide a special case for the simplest inputs.

C) Call the recursive function with simpler inputs.

D) Use more than one return statement.

Ans: B

Title: How can you ensure that a recursive function terminates?

Page 2: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

Difficulty: Easy

Section Ref: 11.1

4. Which of the following is a key requirement to ensure that recursion is successful?

A) Every recursive call must simplify the computation in some way.

B) There should be special cases to handle the most complex computations directly.

C) A recursive function should not call itself except for the simplest inputs.

D) A recursive solution should not be implemented to a problem that can be solved iteratively.

Ans: A

Title: Which is a key requirement for recursion to be successful?

Difficulty: Easy

Section Ref: 11.1

5. What is the output of the following code snippet?

int myfunction(int n)

{

if (n < 2) { return 1; }

return n * myfunction(n - 1);

}

int main()

{

cout << myfunction(3) << endl;

return 0;

}

A) 24

B) 6

C) 2

D) 120

Ans: B

Title: What is output of snippet (with recursive function)?

Difficulty: Medium

Section Ref: 11.1

6. Which of these statements ensures that myfunction terminates for all values of n in the

following code snippet?

int myfunction(int n)

{

Page 3: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

// code goes here

return myfunction(n - 1) + n * n;

}

A) if (n == 1) { return 1; }

B) if (n < 1) { return 1; }

C) if (n == 1) { return 1; }

if (n == 0) { return 0; }

D) if (n == 0) { return 0; }

Ans: B

Title: Which statement ensures that myfunction terminates … in this snippet?

Difficulty: Medium

Section Ref: 11.1

7. Suppose myfunction is defined as follows. How many calls to myfunction are made,

including the first, when myfunction(10) is executed?

int myfunction(int n)

{

if (n <= 2)

{

return 1;

}

return n * myfunction(n - 1);

}

A) 120

B) 9

C) 6

D) 10

Ans: B

Title: How many calls to myfunction result when myfunction(10) is executed?

Difficulty: Hard

Section Ref: 11.1

8. What does this function do?

vector<string> mystery(string word)

{

vector<string> result;

if (word.length() <= 1)

Page 4: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

{

result.push_back(word);

return result;

}

for (int i = 0; i < word.length(); i++)

{

string shorter_word = word.substr(0, i)

+ word.substr(i + 1);

vector<string> shorter_mystery = mystery(shorter_word);

for (int j = 0; j < shorter_mystery.size(); j++)

{

string longer_word = word[i] + shorter_mystery[j];

result.push_back(longer_word);

}

}

return result;

}

A) The code enters a situation where it keeps calling itself without ever ending.

B) The code returns the number of characters in a string.

C) The code generates all permutations of the characters in a string.

D) The code always extracts the first character from the entered string and places the remaining

string as a suffix.

Ans: C

Title: What does this (recursive function using a vector of strings) do?

Difficulty: Hard

Section Ref: 11.1

9. What should be changed in the following code snippet so that it generates permutations of the

characters in a string?

vector<string> generate_permutations(string word)

{

vector<string> result;

if (word.length() <= 1)

{

return result;

}

for (int i = 0; i < word.length(); i++)

{

string shorter_word = word.substr(0, i)

+ word.substr(i + 1);

Page 5: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

vector<string> shorter_permutations =

generate_permutations(shorter_word);

for (int j = 0; j < shorter_permutations.size(); j++)

{

string longer_word = word[i] + shorter_permutations[j];

result.push_back(longer_word);

}

}

return result;

}

A) When word.length() <= 1, add result.push_back(word); before the returning

result.

B) Change result.push_back(longer_word); to result.push_back(shorter_word);.

C) Change string longer_word = word[i] + shorter_permutations[j]; to

string longer_word = word[j] + shorter_permutations[i];

D) No change is required in the code snippet

Ans: A

Title: What should be changed so snippet generates permutations of characters in a string?

Difficulty: Hard

Section Ref: 11.1

10. A word of any length is a palindrome if its characters are the same forward and in reverse.

Which of the following simplifications is helpful in designing a recursive solution to the

problem of checking to see if a word is a palindrome?

A) The word obtained by removing the middle and last letters is a palindrome, and the middle

and last letters are the same.

B) The word obtained by removing the first and last letters is a palindrome, and the first and last

letters are the same.

C) The word obtained by removing the first and middle letters is a palindrome, and the first and

middle letters are the same.

D) The word obtained by removing the first, last, and middle letters is a palindrome, and the first

and last letters are the same.

Ans: B

Title: Which simplification is helpful in designing a recursive solution for this situation?

Difficulty: Easy

Section Ref: 11.3

Page 6: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

11. Which of the following options represents one of the ways for simplifying the input string

when checking whether a string is a palindrome?

A) Removing the first and middle characters from the string.

B) Removing a character from the middle of the string.

C) Removing the first and last characters from the string.

D) Creating two substrings of equal size by reducing the original string to half.

Ans: C

Title: Which is a way of simplifying the input when checking whether a string is a palindrome?

Difficulty: Easy

Section Ref: 11.3

12. What does this function do?

bool myfunction(string s)

{

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

char first = s[0];

char last = s[s.length() - 1];

if (first == last)

{

string shorter = s.substr(1, s.length() - 1);

return myfunction(shorter);

}

else

{

return false;

}

}

A) There is no return value.

B) The code snippet returns true if the string is a palindrome.

C) The code snippet always returns false.

D) The code snippet returns true only for strings of any length consisting of the same character.

Ans: D

Title: What does this (recursive) function (with string parameter) do?

Difficulty: Medium

Section Ref: 11.3

13. Assuming that you need to write a recursive function calc_prod(int n) to calculate the

product of the first n integers, which of the following would be a correct way to simplify the

input for the recursive call?

Page 7: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

A) Call calc_prod(n – 1) and multiply by n.

B) Call calc_prod(n – 2) and multiply by n.

C) Call calc_prod(n + 1) and multiply by n.

D) Call calc_prod(1) and multiply by n.

Ans: A

Title: Which is a correct way to simplify the input for this recursive function?

Difficulty: Medium

Section Ref: 11.3

14. Suppose you need to write a recursive function power(double x, int n) that

calculates x to the power of n. Which of the following would be a correct way to implement

the function power?

A) Call power(x, n - 1) and multiply by n.

B) Call power(x, n) and multiply by (n – 1).

C) Call power(x - 1, n) and multiply by x.

D) Call power(x, n - 1) and multiply by x.

Ans: D

Title: Which is a correct way to implement this recursive function?

Difficulty: Medium

Section Ref: 11.3

15. What is the value of the return variable m in the following code snippet, if a = {1, 2, 3,

4, 5, 6} and n = 5?

int myfunction(int a[], int n)

{

if (n == 1) { return a[0]; }

int m = myfunction(a, n - 1);

if (a[n] > m) { return a[n - 1]; }

else { return m; }

}

A) 1

B) 2

C) 5

D) 6

Page 8: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

Ans: C

Title: What is return value of this recursive function (with these array and integer parameters)?

Difficulty: Hard

Section Ref: 11.3

16. Which of the following statements is true about a recursive function?

I. It makes a recursive call to the function with simpler inputs.

II. It creates a solution to the original problem by reducing it into simpler parts

III. It finds solutions directly for the simplest cases

A) I, II

B) I, III

C) II, III

D) I, II, III

Ans: D

Title: Which statement is true about a recursive function?

Difficulty: Easy

Section Ref: 11.3

17. What is the value of the return statement for the following code snippet, where a = {1,

2, 3, 4, 5, 6, 7, 8} and n = 2?

int myfunction(int a[], int n)

{

if (n == 0)

{

return a[n];

}

return

{

myfunction(a, n - 1) + a[n];

}

}

A) 12

B) 36

C) 6

D) 4

Ans: C

Title: What is return value of this recursive function (with these array and integer arguments)?

Difficulty: Medium

Formatted: Tab stops: Not at 0.25"

Page 9: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

Section Ref: 11.3

18. In the following code snippet, what is the value of the return statement for x = 3 and n =

3?

int myfunction(int x, int n)

{

if (n == 0) { return 1; }

return x * myfunction(x, n - 1);

}

A) 9

B) 27

C) 6

D) 81

Ans: B

Title: What is return value of this recursive function (with these integer arguments)?

Difficulty: Medium

Section Ref: 11.3

19. Recursive functions may involve degenerate inputs. Which of the following can be

considered as a degenerate input?

I. An empty string

II. A shape without any area

III. A negative value for time

A) I, II

B) I, III

C) II, III

D) I, II, and III

Ans: D

Title: Which can be considered as a degenerate input?

Difficulty: Easy

Section Ref: 11.3

20. Examine the code snippet shown below:

bool myfunction(string s)

{

// Test for simplest case goes here

Formatted: Tab stops: Not at 0.25"

Page 10: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

char first = s[0];

char last = s[s.length() - 1];

if (first == last)

{

string shorter = s.substr(1, s.length() - 2);

return myfunction(shorter);

}

else

{

return false;

}

}

What would be the correct test for the simplest inputs so that the function will terminate with the

correct result to determine if a string is a palindrome?

A) if (s.length() <= 1) { return true; }

B) if (s.length() <= 1) { return false; }

C) if (s.length() <= 2) { return true; }

D) if (s.length() <= 3) { return true; }

Ans: A

Title: Which is correct test for the simplest inputs that will terminate this recursion?

Difficulty: Easy

Section Ref: 11.3

21. What is the output of the following code snippet, if a user enters “make” as the input?

void myfunction()

{

char c;

cin.get(c);

if (c == '\n') { return; }

myfunction();

cout << c;

}

A) make

B) maek

C) ekam

D) No output is produced due to infinite recursion.

Ans: C

Title: What is output of (recursive function that reads a string and outputs a string)?

Difficulty: Medium

Page 11: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

Section Ref: 11.3

22. In the following code snippet, what is the return value when the parameter values are m =

12 and n = 8?

int myfunction(int m, int n)

{

if ((m == n) { return n; }

else if (m < n) { return myfunction(m, n - m); }

else { return myfunction(m - n, n); }

}

A) 20

B) 12

C) 8

D) 4

Ans: D

Title: What is return value (of recursive function with if/else)?

Difficulty: Medium

Section Ref: 11.3

23. What is the return value of the following code snippet for n = 3?

string myfunction(int n)

{

string s;

if (n % 2 == 0)

{

s = "0";

}

else

{

s = "1";

}

if (n < 2)

{

return s;

}

return myfunction(n / 2) + s;

}

A) 11

B) 10

C) 01

Page 12: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

D) 00

Ans: A

Title: What is the return value of this recursive function for this input value?

Difficulty: Hard

Section Ref: 11.3

24. Which of the following statements is NOT CORRECT about designing a recursive function?

A) A recursive function design must always consider the effects of multiple nested calls.

B) Each time the recursive function each is called, it will be processing a different value.

C) The function should contain a check for simpler inputs and return a value in those cases.

D) The design should focus on finding a simpler version of the problem being solved.

Ans: A

Title: Which statement is NOT CORRECT about designing a recursive function?

Difficulty: Medium

Section Ref: 11.3

25. Which of the following techniques can make the process of finding a recursive solution

easier?

A) Making a slight change to the original problem so that a helper function can be used.

B) Starting with the simplest inputs and making a recursive call with more complex inputs.

C) Focusing on the pattern of multiple nested calls in the recursive function.

D) First writing an iterative solution and converting it later to recursive.

Ans: A

Title: Which technique can make finding a recursive solution easier?

Difficulty: Easy

Section Ref: 11.3

26. Consider a situation where you need to write a recursive function void reverse that

reverses a string. Suppose your recursive solution removes the first character, reverses the

string consisting of the remaining text, and combines the two. Which of the following would

be a technique that may produce an easier solution?

A) Introduce a helper function that performs string concatenation.

B) Introduce a helper function that reverses a substring of the original string.

Page 13: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

C) Introduce a helper function that removes the first character of a string.

D) Write an iterative function that traverses each character in the string.

Ans: B

Title: Which technique could produce an easier solution to this recursive function?

Difficulty: Medium

Section Ref: 11.4

27. Which of following is true about the reverse_string function used in solving the

problem of reversing a string?

string reverse_substring(string str, int start, int end)

{

if (start >= end)

{

return str;

}

char ch = str[start];

str[start] = str[end];

str[end] = ch;

return reverse_substring(str, start + 1, end - 1);

}

string reverse_string(string str)

{

return reverse_substring(str, 0, str.length() - 1);

}

A) The reverse_string function is a recursive function.

B) The reverse_string function is an iterative function.

C) The reverse_string function uses reverse_substring as a helper function.

D) The reverse_substring function uses reverse_string as a helper function.

Ans: C

Title: Which is true about this function used in solving the problem of reversing a string?

Difficulty: Easy

Section Ref: 11.5

28. Consider a situation where the function reverse_string should reverse a string that is

passed as an argument. Which of the following options correctly completes the

reverse_substring function?

string reverse_substring(string str, int start, int end)

{

if (start >= end)

Page 14: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

{

return str;

}

char ch = str[start];

str[start] = str[end];

str[end] = ch;

return // complete this statement

}

string reverse_string(string str)

{

return reverse_substring(str, 0, str.length() - 1);

}

A) reverse_substring(str, start, end);

B) reverse_substring(str, start + 1, end);

C) reverse_substring(str, start + 1, end - 1);

D) reverse_substring(str, start, end - 1);

Ans: C

Title: Which statement correctly completes the reverse_substring function?

Difficulty: Hard

Section Ref: 11.6

29. Which programming technique is exemplified in the following code snippet?

int multiply(int x, int y, int adder);

int add(int x, int y, int factor)

{

int mval = 1;

int val = x + y;

if (factor > 1)

{

mval = multiply(x, y, factor - 1);

}

return val * mval;

}

int multiply(int x, int y, int adder)

{

int aval = 0;

int val = x * y;

if (adder > 0)

{

aval = add(x, y, adder - 1);

Page 15: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

}

return val + aval;

}

A) The multiply function is a recursive function, but the add function is not recursive.

B) The add function is a recursive function, but the multiply function is not recursive.

C) Both the multiply function and the add function are recursive functions.

D) This technique is mutual recursion because each function calls the other function repeatedly.

Ans: D

Title: Which programming technique is exemplified in this snippet?

Difficulty: Medium

Section Ref: 11.5

30. What is the output of the following code snippet?

int multiply(int x, int y, int adder);

int add(int x, int y, int factor)

{

int mval = 1;

int val = x + y;

if (factor > 1)

{

mval = multiply(x, y, factor - 1);

}

return val * mval;

}

int multiply(int x, int y, int adder)

{

int aval = 0;

int val = x * y;

if (adder > 0)

{

aval = add(x, y, adder - 1);

}

return val + aval;

}

int main()

{

cout << multiply(2, 3, 2) << endl;

return 0;

}

Page 16: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

A) 8

B) 9

C) 11

D) 12

Ans: C

Title: What is output of snippet (using a pair of functions that call each other)?

Difficulty: Medium

Section Ref: 11.5

31. What is the output of the following code snippet?

string clean(string s);

string myfunction(string s)

{

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

string first = s.substr(0, 1);

string shorter = s.substr(1, s.length() - 1);

string cleanstr = clean(shorter);

return cleanstr + first;

}

string clean(string s)

{

string cleanstr;

if (s == "" || s == " ")

{

return "";

}

char ch = s[0];

string sub = s;

while (ch == ' ')

{

sub = sub.substr(1, sub.length());

ch = sub[0];

}

return myfunction(sub);

}

int main()

{

string s = "1 2 3 4 5";

cout << myfunction(s) << endl;

return 0;

}

A) 12345

Page 17: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

B) 54321

C) 5 4 3 2 1

D) 1 2 3 4 5

Ans: B

Title: What is output of snippet (using mutual recursion)?

Difficulty: Hard

Section Ref: 11.6

32. Why does this function perform poorly?

int func(int n)

{

int f;

if (n <= 2)

{

f = 1;

}

else

{

f = func(n - 1) + func(n - 3);

}

return f;

}

A) All recursive functions perform poorly.

B) Multiple calls to the function with the same inputs are made; therefore, it calculates the same

value many times.

C) The check for simple inputs does not check for the correct values; therefore, the function runs

much longer than it needs to.

D) With only a single return statement, it misses many options for ending sooner.

Ans: B

Title: Why does this function perform poorly?

Difficulty: Easy

Section Ref: 11.4

33. Consider a situation where you have written two different functions, both of which determine

whether a string is a palindrome. The first function is a recursive function that calls itself n/2

times for a string of length n. The second function uses an iterative solution that loops n/2

times for a string of length n. Which of the following statements are true about this situation?

A) The recursive function is a much faster solution because a recursive call is always faster than

iteration in a loop.

B) The iterative solution is much faster because performing an operation in a loop is always

Page 18: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

faster than making a function call.

C) The two solutions perform at exactly the same speed.

D) The iterative solution tends to be a little bit faster than the recursive solution.

Ans: D

Title: Which is true (about the relative performance of a recursive and iterative solution)?

Difficulty: Medium

Section Ref: 11.5

34. Consider the two functions that are defined here. Which of the options are true about these

functions?

I. The function f1 is a recursive solution, and the function f2 is an iterative solution

II. The two functions execute at about the same speed

III. For a string of length n, the function f1 makes about n/2 recursive calls but the

function f2 performs about n/2 iterations

string f1(string str)

{

string val = "";

if (str == "" || str.length() <= 1)

{

return str;

}

string first = str.substr(0, 1);

string middle = str.substr(1, str.length() - 2);

string last = str.substr(str.length() - 1, 1);

return return last + f1(middle) + first;

}

string f2(string str)

{

int len = str.length();

int mid = len / 2;

for (int i = 0; i < mid; i++)

{

char ch = str[i];

int pos = len - i - 1;

str[i] = str[pos];

str[pos] = ch;

}

return str;

}

A) I, II

B) I, III

C) II, III

Formatted: Tab stops: Not at 0.25"

Page 19: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

D) I, II, and III

Ans: D

Title: Which is true (about these two recursive/iterative functions)?

Difficulty: Medium

Section Ref: 11.5

35. Which of the following statements are true about the two functions defined here?

string reverse_str(string str)

{

string val = "";

if (str == "" || str.length() <= 1)

{

return str;

}

string first = str.substr(0, 1);

string rest = str.substr(1, str.length() - 1);

val = reverse_str(rest).append(first);

return val;

}

string iter_reverse(string str)

{

int len = str.length();

int mid = len / 2;

for (int i = 0; i < mid; i++)

{

char ch = str[i];

int pos = len - i - 1;

str[i] = str[pos];

str[pos] = ch;

}

return str;

}

A) The reverse function is a recursive solution, and the iter_reverse function is an

iterative solution.

B) The two functions execute at about the same speed.

C) For a string of length n, the reverse function makes about n/2 recursive calls but the

iter_reverse function performs about n/2 iterations.

D) All of the above.

Ans: A

Title: Which is true (about these two recursive/iterative functions)?

Difficulty: Hard

Section Ref: 11.5

Page 20: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

36. Suppose two programmers have written functions that solve the problem of calculating the

Fibonacci number for an input value of n. One of the functions is recursive, and the other is

iterative. In which of the following situations would you prefer using the recursive function?

A) When the recursive function is easier to understand than the iterative function and both

functions perform at about the same speed.

B) When the recursive function performs faster than the iterative function.

C) When the calculation is more complex and the function call overhead can be overlooked.

D) All of the listed options.

Ans: D

Title: In which situation would you prefer a recursive function?

Difficulty: Easy

Section Ref: 11.5

37. What is the output of the following code snippet?

int find(int a, int b)

{

int val;

if (a > b)

{

val = a;

}

else

{

val = b;

}

return val;

}

int find(int a[], int size)

{

int val;

if (size == 1)

{

val = a[0];

}

else

{

val = find(a[size - 1], find(a, size - 1));

}

return val;

}

int main()

{

int a[6] = { 6, 5, 4, 3, 9, 7 };

Page 21: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

cout << find(a, 6);

return 0;

}

A) 3

B) 6

C) 7

D) 9

Ans: D

Title: What is output of snippet (that manipulates an integer array)?

Difficulty: Medium

Section Ref: 11.5

38. What is the value of calculate(4) for the function defined in the following code snippet?

int calculate(int n)

{

if (n <= 1)

{

return 1;

}

return n * calculate(n - 1);

}

A) 4

B) 12

C) 24

D) 42

Ans: C

Title: What is the value of calculate(4) for this function?

Difficulty: Medium

Section Ref: 11.5

39. What is the output of the following code snippet?

string reverse(string str, int start, int end)

{

if (start >= end)

{

return str;

}

char ch = str[start];

str[start] = str[end];

Page 22: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

str[end] = ch;

return reverse(str, start + 1, end - 1);

}

string string_reverse(string str)

{

int index = str.length() / 2;

string str1 = reverse(str, 0, index - 1);

string str2 = reverse(str, index, str.length() - 1);

string str3 = str1.substr(0, index);

string str4 = str2.substr(index, str.length() - index);

return str3.append(str4);

}

int main()

{

cout << string_reverse("1234") << endl;

return 0;

}

A) 1234

B) 4321

C) 4231

D) 2143

Ans: D

Title: What is output of snippet (with helper function)?

Difficulty: Medium

Section Ref: 11.5

40. What is the output of the following code snippet?

vector<string> substrings(string str)

{

vector<string> vec;

if (str == "" || str.length() <= 1)

{

vec.push_back(str);

return vec;

}

string first_char = str.substr(0, 1);

string rest = str.substr(1, str.length() - 1);

vector<string> subs = substrings(rest);

Page 23: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

int size = subs.size();

for (int i = 0; i < size; i++)

{

string s = first_char.substr(0, 1);

s.append(subs[i]);

subs.push_back(s);

}

return subs;

}

int main()

{

vector<string> vec = substrings("123");

for (int i = 0; i < vec.size(); i++)

{

cout << vec[i] << endl;

}

return 0;

}

A) 3

23

13

123

B) 123

13

23

3

C) 23

13

123

D) 123

Ans: A

Title: What is output of snippet (using vector of strings)?

Difficulty: Hard

Section Ref: 11.5

41. What is the output of the following code snippet?

long myrecurse(int n)

{

if (n < 2) { return 1; }

return n * myrecurse(n - 1);

}

int main()

{

Page 24: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

cout << myrecurse(4) << endl;

return 0;

}

A) 48

B) 24

C) 6

D) 3

Ans: B

Title: What is output of snippet (with recursive function)?

Difficulty: Medium

Section Ref: 11.1

42. Which of the options ensures that myrecur terminates for all values of n in the following

code snippet?

int myrecur(int n)

{

// code goes here

return myrecur(n - 1) + n * n;

}

A) if (n == 1) { return 1; }

B) if (n < 1) { return 1; }

C) if (n == 1) { return 1; }

if (n == 0) { return 0; }

D) if (n == 0) { return 0; }

Ans: B

Title: Which statement ensures that myrecur terminates … in this snippet?

Difficulty: Medium

Section Ref: 11.1

43. Suppose myfunction is defined as follows. How many calls to myfunction result,

including the first call, when myfunction(9) is executed?

int myfunction(int n)

{

if (n <= 2)

{

return 1;

Page 25: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

}

return n * myfunction(n - 1);

}

A) 120

B) 9

C) 6

D) 8

Ans: D

Title: How many calls to myfunction result when myfunction(9) is executed?

Difficulty: Hard

Section Ref: 11.1

44. What does this function do?

vector<string> myfunc(string my_string)

{

vector<string> result;

if (my_string.length() <= 1)

{

result.push_back(my_string);

return result;

}

for (int i = 0; i < my_string.length(); i++)

{

string shorter_my_string = my_string.substr(0, i)

+ my_string.substr(i + 1);

vector<string> shorter_myfunc = myfunc(shorter_my_string);

for (int j = 0; j < shorter_myfunc.size(); j++)

{

string longer_my_string = my_string[i]

+ shorter_myfunc[j];

result.push_back(longer_my_string);

}

}

return result;

}

A) The code goes into an infinite loop as it keeps calling itself.

B) The code returns the number of characters in a string.

C) The code generates all permutations of the characters in a string.

D) The code always extracts the first character from the entered string and places the remaining

string as a suffix.

Page 26: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

Ans: C

Title: What does this (recursive function using a vector of strings) do?

Difficulty: Medium

Section Ref: 11.1

45. What should be changed in the following code snippet so that it generates permutations of the

characters in a string?

vector<string> myfunc(string word)

{

vector<string> result;

if (word.length() <= 1)

{

return result;

}

for (int i = 0; i < word.length(); i++)

{

string shorter_word = word.substr(0, i)

+ word.substr(i + 1);

vector<string> my_string = myfunc(shorter_word);

for (int j = 0; j < my_string.size(); j++)

{

string longer_word = word[i] + my_string[j];

result.push_back(longer_word);

}

}

return result;

}

A) When word.length() <= 1, add result.push_back(word); before the returning

result.

B) Change result.push_back(longer_word); to result.push_back(shorter_word);.

C) No change is required in the code snippet.

D) None of the listed options.

Ans: A

Title: What should be changed so snippet generates permutations of characters in a string?

Difficulty: Hard

Section Ref: 11.1

46. What does this function do?

Page 27: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

bool foo(string s)

{

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

char first = s[0];

char last = s[s.length() - 1];

if (first != last)

{

string shorter = s.substr(1, s.length() - 1);

return foo(shorter);

}

else

{

return false;

}

}

A) There is no return value.

B) The code snippet returns false if the string is a palindrome.

C) The code snippet always returns false.

D) The code snippet returns true only for strings that contain only one character repeated any

number of times.

Ans: B

Title: What does this (recursive) function (with string parameter) do?

Difficulty: Medium

Section Ref: 11.3

47. Assuming that you need to write a recursive function foo(int n) to calculate the product

of the first n integers, which of the following would be a correct way to simplify the input for

the recursive call?

A) Call foo(n – 1) and multiply by n.

B) Call foo(n – 3) and multiply by n.

C) Call foo(n - 2) and multiply by n.

D) Call foo(0) and multiply by n.

Ans: A

Title: Which is a correct way to simplify the input for this recursive function?

Difficulty: Medium

Section Ref: 11.3

Page 28: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

48. Suppose you need to write a recursive function foo(double x, int n) that calculates

x to the power of n. Which of the following would be a correct way to implement the

function foo?

A) Call foo(x, n + 1) and multiply by x.

B) Call foo(x, n - 1) and multiply by (n – 1).

C) Call foo(x - 1, n - 1) and multiply by x.

D) Call foo(x, n - 1) and multiply by x.

Ans: D

Title: Which is a correct way to implement this recursive function?

Difficulty: Medium

Section Ref: 11.3

49. What is the value of the return variable m in the following code snippet, if array = {2,

3, 4, 5, 6, 7} and n = 5?

int foo(int array[], int n)

{

if (n == 1) { return array[0]; }

int m = foo(array, n - 1);

if (array[n] > m) { return array[n - 1]; }

else { return m; }

}

A) 2

B) 3

C) 6

D) 7

Ans: C

Title: What is return value of this recursive function (with these array and integer parameters)?

Difficulty: Hard

Section Ref: 11.3

50. What is the value of the return statement for the following code snippet, where a = {3,

4, 5, 6, 7, 8} and n = 5?

int foo(int a[], int n)

{

if (n == 0)

{

return a[n];

}

Page 29: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

return

{

return foo(a, n - 1) + a[n];

}

}

A) 12

B) 36

C) 33

D) 41

Ans: C

Title: What is return value of this recursive function (with these array and integer arguments)?

Difficulty: Medium

Section Ref: 11.3

51. In the following code snippet, what is the value of the return statement for x = 4 and n =

4?

int foo(int x, int n)

{

if (n == 0) { return 1; }

return x * foo(x, n - 1);

}

A) 9

B) 256

C) 346

D) 81

Ans: B

Title: What is return value of this recursive function (with these integer arguments)?

Difficulty: Medium

Section Ref: 11.3

52. What is the output of the following code snippet, if a user enters “mine” as the input?

void foo()

{

char c;

cin.get(c);

if (c == '\n') { return; }

foo();

cout << c;

Page 30: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

}

A) enim

B) enmi

C) mine

D) No output is produced due to infinite recursion.

Ans: A

Title: What is output of (recursive function that reads a string and outputs a string)?

Difficulty: Medium

Section Ref: 11.3

53. In the following code snippet, what is the return value when the parameter values are m =

10 and n = 4?

int foo(int m, int n)

{

if ((m == n) { return n; }

else if (m < n) { return foo(m, n - m); }

else { return foo(m - n, n); }

}

A) 22

B) 2

C) 18

D) 4

Ans: B

Title: What is return value (of recursive function with if/else)?

Difficulty: Medium

Section Ref: 11.3

54. What is the return value of the following code snippet for n = 2?

string foo(int n)

{

string s;

if (n % 2 == 0)

{

s = "0";

}

else

{

Page 31: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

s = "1";

}

if (n < 2)

{

return s;

}

return foo(n / 2) + s;

}

A) 11

B) 10

C) 01

D) 00

Ans: B

Title: What is the return value of this recursive function for this input value?

Difficulty: Hard

Section Ref: 11.3

55. What will be the return value of following code snippet for x = 4 and n = 4?

int foo(int x, int n)

{

if (n == 0) { return 1; }

int p = foo(x, n / 2);

if (n % 2 == 0) { return p * p; }

else { return x * p; }

}

A) 12

B) 64

C) 256

D) 128

Ans: C

Title: What is the return value of this recursive function for these input values?

Difficulty: Medium

Section Ref: 11.5

56. Consider a situation where the function foo should reverse a string that is passed as an

argument. Which of the following options correctly completes the goo function?

string goo(string str, int start, int end)

Page 32: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

{

if (start >= end)

{

return str;

}

char ch = str[start];

str[start] = str[end];

str[end] = ch;

return // Complete this statement

}

string foo(string str)

{

return goo(str, 0, str.length() - 1);

}

A) goo(str, start, end);

B) goo(str, start + 1, end);

C) goo(str, start + 1, end - 1);

D) goo(str, start, end - 1);

Ans: C

Title: Which statement correctly completes the goo function?

Difficulty: Hard

Section Ref: 11.6

57. Consider the two functions that are defined here. Which of the options are true about these

functions?

I. The goo function is a recursive solution, and the foo function is an iterative solution.

II. The two functions execute at about the same speed.

III. For a string of length n, the goo function makes about n/2 recursive calls but the foo

function performs about n/2 iterations

string goo(string str)

{

string val = "";

if (str == "" || str.length() <= 1)

{

return str;

}

string first = str.substr(0, 1);

string middle = str.substr(1, str.length() - 2);

string last = str.substr(str.length() - 1, 1);

Page 33: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

return return last + goo(middle) + first;

}

string foo(string str)

{

int len = str.length();

int mid = len / 2;

for (int i = 0; i < mid; i++)

{

char ch = str[i];

int pos = len - i - 1;

str[i] = str[pos];

str[pos] = ch;

}

return str;

}

A) I, II

B) I, III

C) II, III

D) I, II, and III

Ans: D

Title: Which is true (about these two recursive/iterative functions)?

Difficulty: Medium

Section Ref: 11.6

58. What is the output of the following code snippet?

int find(int a, int b)

{

int val;

if (a > b)

{

val = a;

}

else

{

val = b;

}

return val;

}

int find(int a[], int size)

{

Page 34: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

int val;

if (size == 1)

{

val = a[0];

}

else

{

val = find(a[size - 1], find(a, size - 1));

}

return val;

}

int main()

{

int a[7] = { 6, 5, 4, 3, 9, 2, 1 };

cout << find(a, 7);

return 0;

}

A) 3

B) 6

C) 8

D) 9

Ans: D

Title: What is output of snippet (that manipulates an integer array)?

Difficulty: Easy

Section Ref: 11.6

59. What is the output of the following code snippet?

string reverse(string str, int start, int end)

{

if (start >= end)

{

return str;

}

char ch = str[start];

str[start] = str[end];

str[end] = ch;

return reverse(str, start + 1, end - 1);

}

string string_reverse(string str)

{

Page 35: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

int index = str.length() / 2;

string str1 = reverse(str, 0, index - 1);

string str2 = reverse(str, index, str.length() - 1);

string str3 = str1.substr(0, index);

string str4 = str2.substr(index, str.length() - index);

return str3.append(str4);

}

int main()

{

cout << string_reverse("5678") << endl;

return 0;

}

A) 6578

B) 8765

C) 6587

D) 5687

Ans: C

Title: What is output of snippet (with helper function)?

Difficulty: Medium

Section Ref: 11.6

60. What is the output of the following code snippet?

vector<string> substrings(string str)

{

vector<string> vec;

if (str == "" || str.length() <= 1)

{

vec.push_back(str);

return vec;

}

string first_char = str.substr(0, 1);

string rest = str.substr(1, str.length() - 1);

vector<string> subs = substrings(rest);

int size = subs.size();

for (int i = 0; i < size; i++)

{

Page 36: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

string s = first_char.substr(0, 1);

s.append(subs[i]);

subs.push_back(s);

}

return subs;

}

int main()

{

vector<string> vec = substrings("432");

for (int i = 0; i < vec.size(); i++)

{

cout << vec[i] << endl;

}

return 0;

}

A) 2

32

42

432

B) 32

42

432

C) 2

32

42

D) 32

42

Ans: A

Title: What is output of snippet (using vector of strings)?

Difficulty: Hard

Section Ref: 11.6

61. The Fibonacci numbers are simple to compute recursively, but lead to a computationally

inefficient solution. When tracing that recursive function, what is it about the call tree that

reveals the inefficiency?

A) The call tree shows at least four branches for ever call

B) The call tree shows that the base case is never fully reached

C) The call tree shows that many computations are repeated

D) The call tree is actually a single line

Page 37: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

Ans: C

Title: What about the call tree reveals the inefficiency in the recursive Fibonacci function?

Difficulty: Easy

Section Ref: 11.4

62. The Fibonacci numbers are simple to compute recursively, but lead to a computationally

inefficient solution. What do you observe about the shape of its call tree?

A) The call tree is just one long line

B) Every new level of the call tree has twice as many calls as the level before

C) The call tree creates an infinite loop

D) Every new level of the call tree has half as many calls as the level before

Ans: B

Title: What about the call tree reveals the inefficiency in the recursive Fibonacci function?

Difficulty: Easy

Section Ref: 11.4

63. One way to determine the run-time efficiency of a recursive function is to

A) estimate the shape and size of its call tree

B) count the number of lines of code necessary to implement it

C) check the number of parameter variables that are in the function

D) calculate results with pencil and paper

Ans: A

Title: How can you determine the run-time efficiency of a recursive function?

Difficulty: Easy

Section Ref: 11.4

64. Which statements are true?

I. Sometimes recursive solutions do the same work as iterative solutions

II. Often recursive solutions are easier to implement than their iterative counterparts

III. The performance of recursive functions can always be substantially improved by

converting them to iterative solutions

A) I, II

B) I, III

C) II, III

D) I, II, III

Page 38: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

Ans: A

Title: Which statements about the performance of recursive functions are true?

Difficulty: Easy

Section Ref: 11.4

65. Two quantities a and b are said to be in the golden ratio if

(a b)

ais equal to

a

b. Assuming a

and b are line segments, the golden section is a line segment divided according to the golden

ratio: The total length (a + b) is to the longer segment a as a is to the shorter segment b. One

way to calculate the golden ratio is through the continued fraction:

golden ratio =

11

11

11

1 ...

. Which function below is a correct recursive implementation

of this continued fraction?

A) double golden(int number)

{

if (number < 1) { return 0.0;}

return 1.0 + 1.0 / golden(number - 1);

}

B) double golden(int number)

{

if (number <= 1) { return 0.0;}

return 1.0 + 1.0 / golden(number - 1);

}

C) double golden(int number)

{

if (number <= 1) { return 1.0;}

return 1.0 + 1.0 / golden(number + 1);

}

D) double golden(int number)

{

if (number <= 1) { return 1.0;}

return 1.0 + 1.0 / golden(number - 1);

}

Ans: D

Title: Which recursive function correctly implements the continuing fraction for the golden ratio?

Difficulty: Hard

Section Ref: 11.2

Page 39: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

66. Two quantities a and b are said to be in the golden ratio if

(a b)

a is equal to

a

b. Assuming

a and b are line segments, the golden section is a line segment divided according to the

golden ratio: The total length (a + b) is to the longer segment a as a is to the shorter segment

b. One way to calculate the golden ratio is through the continued fraction:

golden ratio =

11

11

11

1 ...

. In a recursive implementation of this function, what should

be the base case for the recursion?

A) if (number <= 1) { return 0.0;}

B) if (number <= 0) { return 1.0;}

C) if (number <= 1) { return 1.0;}

D) if (number <= 0) { return 0.0;}

Ans: C

Title: What is the base case for a recursive implementation of the golden ratio?

Difficulty: Medium

Section Ref: 11.2

67. Two quantities a and b are said to be in the golden ratio if

(a b)

a is equal to

a

b. Assuming

a and b are line segments, the golden section is a line segment divided according to the

golden ratio: The total length (a + b) is to the longer segment a as a is to the shorter segment

b. One way to calculate the golden ratio is through the continued fraction:

golden ratio =

11

11

11

1 ...

. If the function double golden(int) is a recursive

implementation of this function, what should be the recursive call in that function?

A) return 1.0 / golden(number + 1)

B) return 1.0 + 1.0 / golden(number - 1)

C) return 1.0 / golden(number - 1)

D) return golden(number - 1)

Ans: B

Title: What is the recursive case for a recursive implementation of the golden ratio?

Difficulty: Medium

Page 40: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

Section Ref: 11.2

68. Two quantities a and b are said to be in the golden ratio if

(a b)

ais equal to

a

b. Assuming a

and b are line segments, the golden section is a line segment divided according to the golden

ratio: The total length (a + b) is to the longer segment a as a is to the shorter segment b. One

way to calculate the golden ratio is through the continued square root (also called an infinite

surd): golden ratio =

1 1 1 1 ... . Which function below is a correct recursive

implementation of this continued square root?

A) double golden(int number)

{

if (number <= 1) { return 1.0; }

return sqrt (1.0 + golden(number - 1));

}

B) double golden(int number)

{

if (number <= 1) { return 0.0; }

return sqrt (1.0 + golden(number - 1));

}

C) double golden(int number)

{

if (number <= 1) { return 1.0; }

return sqrt (1.0 + golden(number + 1));

}

D) double golden(int number)

{

if (number <= 1) { return 1.0; }

return pow (1.0 + golden(number - 1), 2.0);

}

Ans: A

Title: Which function correctly implements a recursive function for the continued square root

form of the golden ratio?

Difficulty: Hard

Section Ref: 11.2

Page 41: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

69. Two quantities a and b are said to be in the golden ratio if

(a b)

a is equal to

a

b. Assuming

a and b are line segments, the golden section is a line segment divided according to the

golden ratio: The total length (a + b) is to the longer segment a as a is to the shorter segment

b. One way to calculate the golden ratio is through the continued square root (also called an

infinite surd): golden ratio =

1 1 1 1 ... . In a recursive implementation of this

function, what should be the base case for the recursion?

A) if (number <= 1) { return 0.0;}

B) if (number <= 1) { return sqrt(number);}

C) if (number <= 1) { return 1.0;}

D) if (number <= 1) { return pow(number, 2.0);}

Ans: C

Title: What should be the base case for a recursive implementation of the continued square root

form of the golden ratio?

Difficulty: Medium

Section Ref: 11.2

70. Two quantities a and b are said to be in the golden ratio if

(a b)

a is equal to

a

b. Assuming

a and b are line segments, the golden section is a line segment divided according to the

golden ratio: The total length (a + b) is to the longer segment a as a is to the shorter segment

b. One way to calculate the golden ratio is through the continued square root (also called an

infinite surd): golden ratio =

1 1 1 1 ... . If the function double golden

(int) is a recursive implementation of this function, what should be the recursive call in

that function?

A) return (1.0 + golden(number - 1));

B) return sqrt (1.0 + golden(number - 1));

C) return (1.0 + golden(number));

D) return sqrt (1.0 + golden(number));

Ans: B

Title: What is the recursive case for a recursive implementation of the continued square root form

of the golden ratio?

Difficulty: Medium

Section Ref: 11.2

71. In 1735 Leonard Euler proved a remarkable result, which was the solution to the Basel

Problem, first posed in 1644 by Pietro Mengoli. This result gave a simple expression for

.

Page 42: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

The formula states that

2

6is equal to the limit, as n goes to infinity, of the series

1

11

221

32 ...

1

n2. Can this series be computed recursively?

A) No, because there is no base case

B) No, because the base case is not zero

C) Yes, but the code will be very long

D) Yes

Ans: D

Title: Can pi be computed recursively?

Difficulty: Easy

Section Ref: 11.2

72. In 1735 Leonard Euler proved a remarkable result, which was the solution to the Basel

Problem, first posed in 1644 by Pietro Mengoli. This result gave a simple expression for pi.

The formula states that

2

6 is equal to the limit, as n goes to infinity, of the series

1

11

221

32 ...

1

n2. Which function below is a correct recursive implementation that

approximates this infinite series?

A) double computePI(int number)

{

if (number <= 1) { return 0.0;}

return 1.0 / (number * number) + computePI(number - 1);

}

B) double computePI(int number)

{

if (number <= 1) { return 1.0;}

return 1.0 / (number * number) + computePI(number - 1);

}

C) double computePI(int number)

{

if (number <= 1) { return 0.0;}

return (number * number) + computePI(number - 1);

}

D) double computePI(int number)

{

if (number <= 1) { return 1.0;}

return (number * number) + computePI(number - 1);

}

Ans: B

Page 43: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

Title: Which recursive function correctly computes an approximation to an infinite series?

Difficulty: Medium

Section Ref: 11.2

73. In 1735 Leonard Euler proved a remarkable result, which was the solution to the Basel

Problem, first posed in 1644 by Pietro Mengoli. This result gave a simple expression for pi.

The formula states that

2

6is equal to the limit, as n goes to infinity, of the series

1

11

221

32 ...

1

n2. Which statement below is the correct base case for a recursive

implementation that approximates this infinite series?

A) if (number <= 1) { return 0.0;}

B) if (number <= 1) { return 1.0;}

C) if (number == 0) { return 1.0 / (number * number);}

D) if (number == 1) { return (number * number);}

Ans: B

Title: Which statement correctly computes the base case for an approximation to an infinite

series?

Difficulty: Medium

Section Ref: 11.2

74. In 1735 Leonard Euler proved a remarkable result, which was the solution to the Basel

Problem, first posed in 1644 by Pietro Mengoli. This result gave a simple expression for pi.

The formula states that

2

6 is equal to the limit, as n goes to infinity, of the series

1

11

221

32 ...

1

n2. Which statement below is the recursive case for a recursive

implementation that approximates this infinite series?

A) return 1.0 / (number * number) + computePI(number - 1);

B) return 1.0 / (number * number) + computePI(number);

C) return 1.0 + computePI(number - 1);

D) return 1.0 + computePI(number);

Ans: A

Title: Which statement correctly computes the recursive case for an approximation to an infinite

series?

Difficulty: Medium

Section Ref: 11.2

Page 44: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

75. One remarkably simple formula for calculating the value of

is the so-called Madhava–

Leibniz series:

4 =

11

31

51

71

9 ... Consider the recursive function below to

calculate this formula:

double computePI(int number)

{

if (number <= 1) { return 1.0;}

int oddnum = 2 * number - 1;

return computesign(number) * 1.0 / oddnum

+ computePI(number - 1);

}

In this recursive function, what is the recursive base case?

A) When the parameter variable is less than or equal to one

B) When the parameter variable is greater than one

C) When the parameter variable is zero

D) When the value that is returned from the function is zero

Ans: A

Title: What is the recursive base case for the recursive calculation of the Madhava-Leibniz series?

Difficulty: Easy

Section Ref: 11.2

76. One remarkably simple formula for calculating the value of

is the so-called Madhava–

Leibniz series:

4 =

11

31

51

71

9 ... . Consider the recursive function below to

calculate this formula:

double computePI(int number)

{

if (number <= 1) { return 1.0;}

int oddnum = 2 * number - 1;

return computesign(number) * 1.0 / oddnum

+ computePI(number - 1);

}

In this recursive function, what is the role of the helper function computesign?

A) it is the recursive call in the function

B) it is called just one time to set the sign of the final result

C) it makes sure the sign (positive or negative) alternates as each term of the series is computed

D) it checks the sign of the number and returns true if it is positive and false if negative

Ans: C

Title: What is the role of the helper function in the recursive calculation of the Madhava-Leibniz

series?

Page 45: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

Difficulty: Easy

Section Ref: 11.2

77. One remarkably simple formula for calculating the value of

is the so-called Madhava–

Leibniz series:

4 =

11

31

51

71

9 ... . Consider the recursive function below to

calculate this formula:

double computePI(int number)

{

if (number <= 1) { return 1.0;}

int oddnum = 2 * number - 1;

return computesign(number) * 1.0 / oddnum

+ computePI(number - 1);

}

In this recursive function, how could the helper function computesign be implemented?

A) int computesign(int number)

{

if (number == 0) { return -1; }

return 1;

}

B) int computesign(int number)

{

if (number < 0) { return -1; }

return 1;

}

C) int computesign(int number)

{

if (number / 2 == 0) { return -1; }

return 1;

}

D) int computesign(int number)

{

if (number % 2 == 0) { return -1; }

return 1;

}

Ans: D

Title: How could the helper function be implemented in the recursive calculation of the Madhava-

Leibniz series?

Difficulty: Hard

Section Ref: 11.2

Page 46: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

78. One remarkably simple formula for calculating the value of

is the so-called Madhava–

Leibniz series:

4 =

11

31

51

71

9 ... . Consider the recursive function below to

calculate the first n terms of this formula:

double computePI(int n)

{

if (n <= 1) { return 1.0;}

int oddnum = 2 * n - 1;

if ((n % 2) == 0

{

return -1.0 / oddnum + computePI(n - 1);

}

else

{

return 1.0 / oddnum + computePI(n - 1);

}

}

Which statements about the run-time performance of this function are true?

I. Each time this function is called it will invoke at least two more recursive calls

II. The number of recursive calls this function will make is approximately equal to the value

of the parameter variable n

III. Not counting overhead, this function will be about as efficient as an iterative

implementation of the same formula

A) I, II

B) I, III

C) II, III

D) I, II, III

Ans: C

Title: Which statement is true about the performance of the recursive calculation of the Madhava-

Leibniz series?

Difficulty: Medium

Section Ref: 11.2

79. When a set of cooperating functions call each other in a recursive fashion, it is called

A) permutative recursion

B) recursive chaining

C) beneficial recursion

D) mutual recursion

Page 47: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

Ans: D

Title: What is it called when a set of cooperating functions calls each other in a recursive fashion?

Difficulty: Easy

Section Ref: 11.6

80. One remarkably simple formula for calculating the value of

is the so-called Madhava–

Leibniz series:

4 =

11

31

51

71

9 ... . Consider the recursive function below to

calculate this formula:

double computePI(int number)

{

if (number <= 1) { return 1.0;}

int oddnum = 2 * number - 1;

return computesign(number) * 1.0 / oddnum

+ computePI(number - 1);

}

In this recursive function, suppose the helper function computesign is also implemented

as a recursive function. The relationship of the two functions would then be

A) double recursion

B) mutual recursion

C) nested recursion

D) recurrence relation

Ans: B

Title: What is the term for the relationship of two recursive functions?

Difficulty: Easy

Section Ref: 11.6

81. Recursive leads to a simple-to-understand solution for generating all the permutations of the

letters of a character string. What is the base case for terminating the recursion when

generating all permutations of letters?

A) strings with fewer characters than the original input

B) strings with zero or one character

C) duplicate strings

D) strings with more characters than the original input

Ans: B

Title: What is the base case for the recursive generation of permutations of the letters of a

character string?

Page 48: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

Difficulty: Easy

Section Ref: 11.5

82. Suppose you are given an object that implements a card (stores suit and value) and an object

that implements a hand (five cards). You wish to write a function to write out all possible

permutations of the cards in a hand. What would be the base case?

A) Hands of just one card

B) Cards that are Aces must have priority

C) Hands where all cards are of the same suit

D) Hands of five cards

Ans: A

Title: What is the base case in a recursive solution to permutations of playing cards?

Difficulty: Easy

Section Ref: 11.6

83. Suppose you are given an object that implements a card (stores suit and value) and an object

that implements a hand (five cards). You wish to write a function to write out all possible

permutations of the cards in a hand. How could your recursive solution store the

permutations?

A) Vector of cards

B) Vector of hands

C) Vector of strings

D) Vector of suits

Ans: B

Title: What structure can help in the recursive solution to permutations of playing cards?

Difficulty: Easy

Section Ref: 11.6

84. Study the functions below:

int myfun1(int n)

{

if (n < 1) { return 0; }

return myfun2(n * 2 - 1);

}

int myfun2(int n)

Page 49: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

{

if (n == 1) { return 1;}

return n + myfun2(n-2);

}

What does the function myfun1 compute for positive n?

A) Sum of the first n integers

B) Average of the first n integers

C) Sum of the first n odd integers

D) Average of the first n odd integers

Ans: C

Title: What does the recursive function with helper function compute?

Difficulty: Medium

Section Ref: 11.3

85. Study the functions below:

int myfun1(int n)

{

if (n < 1) { return 0; }

return myfun2(n * 2 - 1);

}

int myfun2(int n)

{

if (n == 1) { return 1;}

return n + myfun2(n - 2);

}

Which of the following statements are true?

I. myfun1 is recursive

II. myfun2 is recursive

III. myfun1 is a recursive helper function

A) I, II

B) I, III

C) II, III

D) I, II, III

Ans: C

Title: Which statements about specific recursive functions are true?

Difficulty: Medium

Section Ref: 11.3

Page 50: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

86. Study the functions below:

int myfun1(int n)

{

if (n < 1) { return 0; }

return myfun2(n * 2 - 1);

}

int myfun2(int n)

{

if (n == 1) { return 1;}

return n + myfun2(n - 2);

}

What is the output for the statement cout << myfun1(7)?

A) 25

B) 26

C) 0

D) 49

Ans: D

Title: What is the output from the given (recursive) functions?

Difficulty: Medium

Section Ref: 11.3

87. Study the functions below:

int myfun1(int n)

{

if (n < 1) { return 0; }

return myfun2(n * 2 - 1);

}

int myfun2(int n)

{

if (n == 1) { return 1;}

return n + myfun2(n - 2);

}

What is the output for the statement cout << myfun1(-7)?

A) 25

B) 26

C) 0

Page 51: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

D) 1

Ans: C

Title: What is the output from the given (recursive) functions?

Difficulty: Medium

Section Ref: 11.3

88. A word-unit palindrome is a sentence that produces the same words forward or backward.

For example, "Fall leaves after leaves fall" is a word-unit palindrome. Suppose you wish to

implement a recursive function that can check sentences to see if they are word-unit

palindromes. What is the first step?

A) combine simple inputs to form a composite solution

B) determine the base case

C) design an object as a wrapper class to solve the problem

D) break the input into smaller parts that can themselves be inputs to the problem

Ans: D

Title: What is the first step in solving a recursive problem?

Difficulty: Easy

Section Ref: 11.2

89. A word-unit palindrome is a sentence that produces the same words forward or backward.

For example, "Please me by standing by me please" is a word-unit palindrome. Suppose you

wish to implement a recursive function that can check sentences to see if they are word-unit

palindromes. Which answer below could be a base case?

A) Words that are the same forward and backward

B) Words that have only one letter

C) Sentences that have only one word

D) Sentences that produce the same words forward or backward

Ans: C

Title: What is the base case in solving a recursive problem?

Difficulty: Medium

Section Ref: 11.2

90. When tracing the execution of a recursive function, what can remove confusion about the

current point of execution when debugging the program?

A) examination of the call stack

Page 52: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

B) memory trace

C) deference pointer variables

D) reference versus value parameter variables

Ans: A

Title: What help reduce confusion when tracing recursive functions?

Difficulty: Easy

Section Ref: 11.1

91. Infinite recursion can lead to an error known as

A) stack fault

B) memory exception

C) syntax error

D) logic error

Ans: A

Title: Infinite recursion can lead to what error?

Difficulty: Easy

Section Ref: 11.1

92. Infinite recursion can occur because

A) the base case is missing one of the necessary termination conditions

B) the recursive case is invoked with simpler arguments

C) a second function is called from the recursive one

D) the recursive function is called more than once

Ans: A

Title: When can infinite recursion occur?

Difficulty: Easy

Section Ref: 11.1

93. Suppose you are to write a recursive function to calculate the "area" of a triangle shown as

follows, where the area of each [] square is 1:

[][][][][][][][][]

[][][][][][][]

[][][][][]

[][][]

Page 53: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

[]

Assume the base of the triangle (top edge) always has an odd number of [] squares, and each

subsequent line has two fewer squares (and hence the triangle always ends with a point that

has just one [] square). What could be an appropriate base case for a recursive function that

calculates the area of a triangle of this kind?

A) if (side_length % 2 == 1) { return 1; }

B) if (side_length == 1) { return 1; }

C) if (side_length == 0) { return 1; }

D) if (side_length > 1) { return side_length; }

Ans: B

Title: What is the base case for a recursive "area of triangle" function?

Difficulty: Easy

Section Ref: 11.1

94. Suppose you are to write a recursive function to calculate the "area" of a triangle shown as

follows, where the area of each [] square is 1:

[][][][][][][][][]

[][][][][][][]

[][][][][]

[][][]

[]

Assume the base of the triangle (top edge) always has an odd number of [] squares, and each

subsequent line has two fewer squares (and hence the triangle always ends with a point that

has just one [] square). What other problem is equivalent to this area computation?

A) sum of the first n integers

B) sum of the first n even integers

C) sum of the first n odd integers

D) sum of every third integer

Ans: B

Title: What is the base case for a recursive "area of triangle" function?

Difficulty: Easy

Section Ref: 11.1

95. Suppose you are to write a recursive function to calculate the "area" of a triangle shown as

follows, where the area of each [] square is 1:

[][][][][][][][][]

[][][][][][][]

Page 54: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

[][][][][]

[][][]

[]

Assume the base of the triangle (top edge) always has an odd number of [] squares, and each

subsequent line has two fewer squares (and hence the triangle always ends with a point that

has just one [] square). The recursive step is to observe that the area of this triangle is

A) one, plus the length of the next side

B) the height times the width divided by two

C) the length of the longest side plus the next longest side, which is two units shorter

D) the length of the longest side plus the area of the smaller triangle that remains after removing

that side

Ans: D

Title: What is the base case for a recursive "area of triangle" function?

Difficulty: Medium

Section Ref: 11.1

96. Suppose you are to write a recursive function to calculate the "area" of a triangle shown as

follows, where the area of each [] square is 1:

[][][][][][][][][]

[][][][][][][]

[][][][][]

[][][]

[]

Assume the base of the triangle (top edge) always has an odd number of [] squares, and each

subsequent line has two fewer squares (and hence the triangle always ends with a point that

has just one [] square). Consider the recursive function below:

1. int triangle_area(int side_length)

2. {

3. if (side_length <= 0) { return 0; }

4. if (side_length == 1) { return 1; }

5. int smaller_side_length = side_length - 1;

6. int smaller_area = triangle_area(smaller_side_length);

7. return smaller_area + side_length;

8. }

Will this function correctly compute the area of triangles as shown above for odd side length?

A) Yes

B) no, because line 4 should be if (side_length == 1) { return 2; }

C) no, because line 5 should be int smaller_side_length = side_length - 2;

D) no, because line 7 should be return smaller_area + side_length – 2;

Page 55: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

Ans: C

Title: Will the given recursive function correctly compute areas of triangles as specified?

Difficulty: Medium

Section Ref: 11.1

97. Consider the following recursive function:

1. int triangle_area(int side_length)

2. {

3. if (side_length <= 0) { return 0; }

4. if (side_length == 1) { return 1; }

5. int smaller_side_length = side_length - 2;

6. int smaller_area = triangle_area(smaller_side_length);

7. return smaller_area + side_length;

8. }

What is returned when this function is called with a value for side_length of 7?

A) 16

B) 28

C) 0

D) 1

Ans: A

Title: What is returned from a call to the given recursive function?

Difficulty: Easy

Section Ref: 11.1

98. Two quantities a and b are said to be in the golden ratio if

(a b)

ais equal to

a

b. Assuming a

and b are line segments, the golden section is a line segment divided according to the golden

ratio: The total length (a + b) is to the longer segment a as a is to the shorter segment b. It

turns out that the ratios of successive terms of the Fibonacci sequence approximate the

golden ratio. That is to say

F(n 1)

F(n) is, in the limit, the golden ratio, where

F(n) is the nth

number of the Fibonacci sequence. Consider the function below that computes an

approximation to the golden ratio using the (n-1) and (n-2) numbers in the Fibonacci

sequence:

double golden(int n)

{

double ratio;

if (n <= 2) { ratio = 1.0; }

else { ratio = ((1.0) * fib(n - 1)) / (1.0 * fib(n - 2)); }

return ratio;

Page 56: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

}

Which statements are true?

I. The function golden is recursive

II. The function goldenis a helper function

III. The function goldenassumes that another function fib computes the Fibonacci

numbers required to compute the ratio

A) I, II

B) I, III

C) II, III

D) I, II, III

Ans: C

Title: Which statements about a set of functions that approximate the golden ratio are true?

Difficulty: Easy

Section Ref: 11.4

99. Consider the following recursive function:

void myfun(string word)

{

if (word.length() == 0) { return; }

myfun(word.substr(1, word.length()));

cout << word[0];

}

What does this function do?

A) Prints the string word

B) Prints the string word in reverse

C) Prints the string word both forward and reverse

D) Prints the length of the string word

Ans: B

Title: What does the given recursive function do?

Difficulty: Easy

Section Ref: 11.1

100. Consider the following recursive function:

1. void myfun(string word)

2. {

3. if (word.length() == 0) { return; }

4. myfun(word.substr(1, word.length()));

5. cout << word[0];

6. }

Page 57: Chapter11: Recursion Multiple Choice - CS215 - homecs215.wikispaces.com/file/view/ch11+Rec.pdfChapter11: Recursion Multiple Choice 1. Which of the following statements is correct about

What changes about this function if lines 4 and 5 are swapped?

A) nothing

B) creates infinite recursion

C) prints the characters of the string in both forward and reverse order

D) reverses the order in which the characters of the string are printed

Ans: D

Title: What changes about the output if lines of the function are swapped?

Difficulty: Easy

Section Ref: 11.1