Sample Test Questions for Chapter 15
1) If the base condition is not defined in the recursive function, _____.
a. the program runs only once
b. the program runs as many times as the number passed as its argument
c. the program terminates giving an error
d. the program gets into an infinite loop
2) What is output?
def divide_by_two(count):
if count == 1:
print('Terminated..!')
else:
print(count)
divide_by_two(count/2)
divide_by_two(9)
a. 9
4.5
2.25
1.125
Terminated..!
b. 9
4.5
2.25
Terminated..!
c. Infinite loop
d. 9
4.5
2.25
1.125
0.5625
Terminated..!
3) How many times is the function count_down() called in the below code?
def count_down(count):
if count == 1:
print('Terminated..!')
else:
print(count)
count_down(count-1)
count_down(5)
a. 5
b. 6
c. 4
d. 3
4) Define a base condition to find the sum of arithmetic progression of the function.
def arith_sum(a1, diff, nth):
XXX
return 0
else:
return a1 + arith_sum(a1 + diff, diff, nth-1)
a. if nth == 0:
b. if nth >= 0:
c. if a1 >= 0:
d. if a1 == 0:
5) Complete the code to get a factorial of a number.
def factorial(number):
if number == 0:
return 1
else:
XXX
print(factorial(4))
a. return number*(number-1)
b. return factorial(number)*factorial(number-1)
c. return number*factorial(number-1)
d. return (number-1)*(number-2)
6) How many times is the recursive function find() called when searching for the missing letter 'A' in the
below code?
def find(lst, item, low, high):
range_size = (high - low) + 1
mid = (high + low) // 2
if item == lst[mid]:
pos = mid
elif range_size == 1:
pos = -1
else:
if item < lst[mid]:
pos = find(lst, item, low, mid)
else:
pos = find(lst, item, mid+1, high)
return pos
listOfLetters = ['B', 'C', 'D', 'E', 'F', 'G', 'H']
print(find(listOfLetters, 'A', 0, 6))
a. 4
b. 3
c. 5
d. program gets into an infinite loop
7) Assume that there is a recursive binary search function find(). If a sorted list has a data structure with
indices 0 to 50 and the item being searched for happens to be at location 6, write each call of find() that
would occur while searching for that item. The first is find(0,50).
a. find(0, 25) find(0, 12) find(0, 6)
b. find(0, 25) find(0, 12)
c. find(0, 25)
d. find(0, 25) find(0, 12) find(0, 6) find(0, 3)
8) What is output if the code is executed to search for the letter 'A'?
def Find(list, ele, low, high):
if high >= low:
mid = (high + low)//2
if list[mid] == ele:
return mid
elif list[mid] > ele:
return Find(list, ele, low, mid-1)
else:
return Find(list, ele, mid + 1, high)
else:
return -1
listOfLetters = ['B', 'C', 'D', 'E', 'F', 'G', 'H']
result = Find(listOfLetters,'A',0,(len(listOfLetters)-1))
print(result)
a. 3
b. Error: The program gets into an infinite loop
c. 0
d. -1
9) Which XXX completes the find function?
def Find(list, ele, low, high):
if high >= low:
XXX
if list[mid] == ele:
return mid
elif list[mid] > ele:
return Find(list, ele, low, mid-1)
else:
return Find(list, ele, mid + 1, high)
else:
return -1
a. mid = low - (high - low) // 2
b. mid = (high - low) // 2
c. mid = (high - low) / 2
d. mid = low + (high - low) // 2
10) How many iterations are needed for the function to find 12?
def Find(list, ele, low, high):
if high >= low:
mid = (high + low)//2
if list[mid] == ele:
return mid
elif list[mid] > ele:
return Find(list, ele, low, mid-1)
else:
return Find(list, ele, mid + 1, high)
else:
return -1
listOfNumbers = [11, 12, 13, 15, 18]
result = Find(listOfNumbers,12,0,(len(listOfNumbers)-1))
print(result)
a. 4
b. 3
c. 2
d. 1
11) Which is the best way to debug recursive functions?
a. Adding print statement of what that line of code does.
b. Adding output statements by keeping all the statements left aligned.
c. Adding output statements with an indent to print statements at every iteration.
d. Adding output statements by keeping all the statements equally indented.
12) While adding output statements to debug recursive functions, _____ the print statements to show the
current depth of recursion.
a. left align
b. indent
c. right align
d. center align
13) An indent variable _____ number of spaces on each iteration.
a. adds unequal
b. removes unequal
c. removes equal
d. adds equal
14) A base case is a _____.
a. case that returns a value without performing a recursive call
b. function method that calls itself
c. function execution instance that calls another execution instance of the same function
d. case that calls another function method
15) Which condition is the recursive case for sum of the first n natural numbers?
def nsum(n):
if n == 0:
sum = 0
elif n == 1:
sum = 1
else:
sum = n + nsum(n-1)
return sum
a. if n == 0
b. sum = n + nsum(n-1)
c. elif n == 1
d. sum = 1
16) How many times is the function Info() called if the user enters 0 and 1 in the first attempt?
def Info(num1, num2):
if (num1 == 0) or (num1 == 1):
print('Passed round 1.')
if (num2 == 1) or (num2 == 0):
print('Passed round 2.')
print('Good. You know your binary digits!')
else:
print('Numbers can be only 0s or 1s...Enter again')
arg1 = int(input())
arg2 = int(input())
Info(arg1, arg2)
else:
print('Numbers can be only 0s or 1s...Enter again')
arg1 = int(input())
arg2 = int(input())
Info(arg1, arg2)
print('Enter 2 numbers used in binary notation')
user_val1 = int(input())
user_val2 = int(input())
Info(user_val1, user_val2)
a. 1
b. 2
c. 0
d. none
17) What is output?
def sub(i,j):
if(i==0):
return j
else:
return sub(i-1,j-i)
print(sub(4,10))
a. 20
b. -3
c. 0
d. Infinite loop
18) What is the output of the following code if the user enters -1 and 6?
def Greater(num1, num2):
if (num1 >= num2):
print('Number 1 is greater')
else:
print('Number 2 is greater')
def Info(num1, num2):
if (num1 < = 0):
print('Number 1 cannot be less than or equal to 0...Enter the number
again')
arg1 = int(input())
arg2 = num2
Info(arg1, arg2)
elif (num2 <= 0):
print('Number 2 cannot be less than or equal to 0...Enter the number
again')
arg2 = int(input())
arg1 = num1
Info(arg1, arg2)
else:
Greater(num1, num2)
print('Enter any 2 numbers')
user_val1 = int(input())
user_val2 = int(input())
Info(user_val1, user_val2)
a. The programs asks the user to enter the second number again.
b. The programs asks the user to enter both numbers again
c. The programs asks the user to enter the first number again
d. Infinite loop
19) What is output?
def test(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return test(n-1)+test(n-2)
for i in range(0,4):
print(test(i),end=' ')
a. 0 1 2 3
b. 1 2 3 4
c. 0 1 1 2 3
d. 0 1 1 2
20) What does the following function print for n1=12 and n2=15?
def div(n1, n2):
if n1 % n2 == 0:
return n2
else:
return div(n2,n1%n2)
print(div(12,15))
a. 12
b. 15
c. 3
d. 9
21) What is the role of [Link]()?
a. It measures the maximum depth of the function.
b. It changes the depth of the function.
c. It isolates the defined function.
d. It finds the error of the function.
22) Which of the following is a good candidate for using recursive functions?
a. To solve the greatest common divisor (GCD) problem
b. To solve problems that have a true and false solution
c. To solve problems that require excessive memory allocation
d. To solve logarithmic problems
23) Which code calculates the power of a number raised to another (a^b)?
a. def power(a,b):
if b == 1:
return a
else:
return a**power(a,b-1)
b. def power(a,b):
if b == 1:
return a
else:
return a*power(b-1,a)
c. def power(a,b):
if b == 1:
return a
else:
return a**power(b-1,a)
d. def power(a,b):
if b == 1:
return a
else:
return a*power(a,b-1)
24) Recursive exploration does not explore _____.
a. all possible reorderings of a word's letters
b. all the subsets of items
c. all possible paths between cities
d. only one possible choice
25) What is the output ofscramble('ab', '')in the following code?
def scramble(r_letters, s_letters):
if len(r_letters) == 0:
print(s_letters)
else:
for i in range(len(r_letters)):
scramble_letter = r_letters[i]
remaining_letters = r_letters[:i] + r_letters[i+1:]
scramble(remaining_letters, s_letters + scramble_letter)
scramble('ab','')
a. ab
ba
b. null
c. ab
null
d. Null
ab
26) Identify the error in the code that scrambles a word’s letters in all the possible ways..
def scramble(r_letters, s_letters):
if len(r_letters) == 0:
print(s_letters)
else:
for i in range(len(r_letters)):
scramble_letter = r_letters[i]
remaining_letters = r_letters[:i] + r_letters[i-1:]
scramble(remaining_letters, s_letters + scramble_letter)
scramble('abc','')
a. scramble_letter = r_letters[i]
b. remaining_letters = r_letters[:i] + r_letters[i-1:]
c. scramble(remaining_letters, s_letters + scramble_letter)
d. if len(r_letters) == 0
27) A traveller starts his journey from Chicago and travels to Los Angeles, New York, California, and
Florida. Which XXX is the best recursive exploration method to find the distance the traveller travelled?
def distance_travel(curr_path, need_to_visit):
if len(curr_path) == num_cities:
total_distance = 0
for i in range(len(curr_path)):
print(f'{city_names[curr_path[i]]} ', end=' ')
if i > 0:
total_distance += distances[curr_path[i-1]][curr_path[i]]
print(f'= {total_distance}')
else:
for i in range(len(need_to_visit)):
city = need_to_visit[i]
XXX
need_to_visit.insert(i, city)
curr_path.pop()
a. distance_travel(curr_path, need_to_visit)
need_to_visit.pop(i)
curr_path.append(city)
b. distance_travel(curr_path, need_to_visit)
need_to_visit.append(city)
curr_path.pop(i)
c. need_to_visit.append(city)
curr_path.pop(i)
distance_travel(curr_path, need_to_visit)
d. need_to_visit.pop(i)
curr_path.append(city)
distance_travel(curr_path, need_to_visit)