what are the base cases in
the following recursive
function?
def recfunction(n):
if n>0:
print(n%10)
recfunction(n//10)
n>0
n<=0
no base cases
n<0
Answers
Explanation:
15.2 Fill in the code to complete the following function for computing factorial.
def factorial(n):
if n == 0: # Base case
return 1
else:
return _____________________ # Recursive call
A. n * (n - 1)
B. n
C. n * factorial(n - 1)
D. factorial(n - 1) * n
15.3 What are the base cases in the following recursive function?
def xfunction(n):
if n > 0:
print(n % 10)
xfunction(n // 10)
A. n > 0
B. n <= 0
C. no base cases
D. n < 0
15.4 Analyze the following recursive function.
def factorial(n):
return n * factorial(n - 1)
A. Invoking factorial(0) returns 0.
B. Invoking factorial(1) returns 1.
C. Invoking factorial(2) returns 2.
D. Invoking factorial(3) returns 6.
E. The function runs infinitely and causes a StackOverflowError.
15.5 How many times is the factorial function in Listing 15.1 invoked for factorial(5)?
A. 3
B. 4
C. 5
D. 6
Section 15.3 Problem: Computing Fibonacci Numbers
15.6 Which of the following statements are true?
A. The Fibonacci series begins with 0 and 1, and each subsequent number is the sum of the preceding two numbers in the series.
B. The Fibonacci series begins with 1 and 1, and each subsequent number is the sum of the preceding two numbers in the series.
C. The Fibonacci series begins with 1 and 2, and each subsequent number is the sum of the preceding two numbers in the series.
D. The Fibonacci series begins with 2 and 3, and each subsequent number is the sum of the preceding two numbers in the series.
15.7 How many times is the fib function in Listing 15.2 invoked for fib(5)?
A. 14
B. 15
C. 25
D. 31
E. 32
15.8 Fill in the code to complete the following function for computing a Fibonacci number.
def fib(index):
if index == 0: # Base case
return 0
elif index == 1: # Base case
return 1
else: # Reduction and recursive calls
return _________________________
A. fib(index - 1)
B. fib(index - 2)
C. fib(index - 1) + fib(index - 2)
D. fib(index - 2) + fib(index - 1)
Section 15.4 Problem Solving Using Recursion
15.9 In the following function, what is the base case?
def xfunction(n):
if n == 1:
return 1
else
return n + xfunction(n - 1)
A. n is 1.
B. n is greater than 1.
C. n is less than 1.
D. no base case.
15.10 What is the return value for xfunction(4) after calling the following function?
def xfunction(n):
if n == 1:
return 1;
else:
return n + xfunction(n - 1)
A. 12
B. 11
C. 10
D. 9
15.11 Fill in the code to complete the following function for checking whether a string is a palindrome.
def isPalindrome(s):
if len(s) <= 1: # Base case
return True
elif _____________________________
return False
else:
return isPalindrome(s.substring(1, len(s) - 1))
A. s[0] != s[-1]: # Base case
B. s[0] != s[len(s)]: # Base case
C. s[1] != s[len(s) - 1]: # Base case
D. s[1] != s[len(s)]: # Base case
15.12 Analyze the following code:
def xfunction(x, length):
print(x[length - 1], end = " ")
xfunction(x, length - 1)
x = [1, 2, 3, 4, 5]
xfunction(x, 5)
A. The program displays 1 2 3 4 6.
B. The program displays 1 2 3 4 5 and then raises an index out of range exception.
C. The program displays 5 4 3 2 1.
D. The program displays 5 4 3 2 1 and then raises an index out of range exception.
Section 15.5 Recursive Helper functions
15.13 Fill in the code to complete the following function for checking whether a string is a palindrome.
def isPalindrome(s):
return isPalindromeHelper(s, 0, len(s) - 1)
def isPalindromeHelper(s, low, high):
if high <= low: # Base case
return True
elif s[low] != s[high]: # Base case
return False
else:
return ____________________________
A. isPalindromeHelper(s)
B. isPalindromeHelper(s, low, high)
C. isPalindromeHelper(s, low + 1, high)
D. isPalindromeHelper(s, low, high - 1)
E. isPalindromeHelper(s, low + 1, high - 1)
15.14 Fill in the code to complete the following function for sorting a list.
def sort(lst):
_________________________ # Sort the entire list
def sortHelper(lst, low, high):
if low < high:
# Find the smallest number and its index in lst[low .. high]
indexOfMin = low
min = lst[low]
for i in range(low + 1, high + 1):
if lst[i] < min:
min = lst[i]
indexOfMin = i
# Swap the smallest in list(low .. high) with list(low)
lst[indexOfMin] = lst[low]
lst[low] = min
# Sort the remaining list(low+1 .. high)
sortHelper(lst, low + 1, high)
A. sortHelper(lst)
B. sortHelper(lst, len(lst) - 1)
C. sortHelper(lst, 0, len(lst) - 1)
D. sortHelper(lst, 0, len(lst) - 2)
I can find this on search engine