Question: Question 1 Consider the following function: 1. def mystery(n, m) : 2.3.4.5.6.What will happen if lines #2 and #3 were swapped with lines #4 and
Question 1
Consider the following function:
1. def mystery(n, m) :
2.3.4.5.6.What will happen if lines #2 and #3 were swapped with lines #4 and #5?
Question options:
The original function and the modified function will return the same result for all integer values of n and m. The original function and the modified function will return different results for all integer value of n and m.
The original function and the modified function will return the same result when n is greater than m, and different results when m is greater than n.
The original function and the modified function will return the same result when n is less than m, and different results when m is less than n.
Question 2
Consider the following code segment:
def sumList(data) :
return sumListIndex(data, 0)
def sumListIndex(data, i) :
if i == len(data) :
return 0
else :
return data[i] + sumListIndex(data, i + 1)
This code segment contains an example of
Question options:
| a recursive helper function | |
| backtracking | |
| iteration | |
| mutual recursion |
Question 3
Consider the following recursive code snippet:
1. def mystery(n, m) :
2. if n <= 0 :
3. return 0
4. if n == 1 :
5. return m
6. return m + mystery(n - 1, m)
Identify the terminating condition(s) of function mystery?
Question options:
| n <= 0 | |
| n == 1 | |
| n <= 0 or n == 1 | |
| n > 0 |
Question 4
How many squares are drawn by the following code segment?
def tSquare(width, x, y, canvas) :
canvas.drawRect(x, y, width, width)
if width >= 4 :
tSquare(width / 2, x, y, canvas)
tSquare(width / 2, x + width / 2, canvas)
tSquare(width / 2, x, y + width / 2, canvas)
tSquare(width / 2, x + width / 2, y + width / 2, canvas)
# Code to setup the canvas has been omitted
tSquare(0, 0, 8, canvas)
Question options:
| 1 | |
| 4 | |
| 5 | |
| 8 |
Question 5
Complete the code for the myFactorial recursive function shown below, which is intended to compute the factorial of the value passed to the function:
1. def myFactorial(n) :
2. if _____________________________ :
3. return 1
4. else :
5. return n * myFactorial(n - 1)
Question options:
| n == 1 | |
| (n - 1) == 1 | |
| n * (n - 1) == 1 | |
| myFactorial(n) == 1 |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
