Question: PYTHON MULTI CHOICE 1a) Consider the following recursive function: def myPrint(n): if n < 10: print(n) else: m = n % 10 print(m) myPrint(n //

PYTHON MULTI CHOICE 1a) Consider the following recursive function:

 def myPrint(n): if n < 10: print(n) else: m = n % 10 print(m) myPrint(n // 10) 

What does this function do?

It prints a positive value forward, digit by digit

It prints a positive value backward, digit by digit

It divides the number by 10 and prints out its last digit
It divides the number by 10 and prints out the result

1b) Complete the code for the recursive function printSum shown in this code snippet, which is intended to return the sum of digits from 1 to n:

1. def printSum(n): 2. if n == 0: 3. return 0 4. else: 5. ________________________________ 
return printSum(n - 1) 
return n + printSum(n + 1) 
return n + printSum(n - 1) 
return n - printSum(n - 1) 

1c) This function is supposed to recursively compute x to the power n, where x and n are both non-negative integers:

1. def power(x, n): 2. if n == 0: 3. ________________________________ 4. else: 5. return x * power(x, n - 1) 

What code should be placed in the blank to accomplish this goal?

return 1 
return x 
return power(x, n - 1) 
return x * power(x, n - 1) 

1d) Consider the following code segment:

def fib(n): # Line 1 if n <= 2: return 1 # Line 2 else: return fib(n - 1) + fib(n - 2) # Line 3 print(fib(6)) # Line 4 

Which line is the base case for this recursive function?

Line 1
Line 2
Line 3
Line 4

1e) The following function is supposed to use recursion to compute the area of a square from the length of its sides. For example, squareArea(3) should return 9.

def squareArea(sideLength) : if sideLength == 1 : return 1 else : ____________________ 

What line of code should be placed in the blank to achieve this goal?

return squareArea(sideLength - 1) 
return 2 * squareArea(sideLength - 1) 
return 2 * sideLength + squareArea(sideLength -1) 
return 2 * sideLength - 1 + squareArea(sideLength -1) 

1f) How many recursive calls to the fib function shown below would be made from an original call to fib(4)? (Do not count the original call)

1. def fib(n): 2. # assumes n >= 0 3. if n <= 1: 4. return n 5. else: 6. return fib(n - 1) + fib(n - 2) 
1
2
4
8

1g) Consider the function powerOfTwo shown below:

1. def powerOfTwo(n) : 2. if n == 1 : 3. return True 4. elif n % 2 == 1 : 5. return False 6. else : 7. return powerOfTwo(n / 2) 

What is the best interpretation of lines 2 and 3?

One is a power of two.
One is not a power of two.
Any multiple of one is a power of two.
The integer 1 is an invalid choice for n.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!