Question: Question 1 Consider the following function: 1. def mystery(n, m) : 2. if n == 0 : # special case 1 3. return 0 4.
Question 1
Consider the following function:
1. def mystery(n, m) :
2. if n == 0 : # special case 1
3. return 0
4. if n == 1 : # special case 2
5. return m
6. return m + mystery(n - 1), m)
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 f1(n) :
if n < 0 :
return 0
if n % 2 == 1 :
return n
return f2(n + 1)
def f2(n) :
if n < 0 :
return 0
if n % 2 == 0 :
return n
return f1(n // 2)
print(f2(7))
When this code is run, it will display:
Question options:
| 0 | |
| 1 | |
| 3 | |
| 7 |
Question 3
A particular algorithm visits n3 + nlog(n) + n! elements in order to perform its task on a list of n elements. Which big-Oh expression best describes the growth rate of this algorithm?
Question options:
| O(n) | |
| O(n3) | |
| O(n!) | |
| O(nlog(n)) |
Question 4
What is wrong with the following classes?
class Person : ...
def getName(self) : return self._name
...class Physician(Person) :
...def getName(self) :
return "Dr. " + self.getName() ...
Question options:
The return statement cannot include string concatenationThe
Physician class cannot contain a method named getNameThe
body of the getName method in Physician contains a logic error
Physician is not a subclass of Person
Question 5
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 6
Consider the following recursive code snippet:
1.2.3.4.5.6.Identify the terminating condition(s) of function mystery?
Question options:
def mystery(n, m) : ifn<=0:
return 0 ifn==1: return m
return m + mystery(n - 1, m)
Question options
n <= 0
n == 1
n <= 0 or n == 1
n>0
Question 7
In the textbook, the line now represented by the blank was for i in range(len(word)) :. What would happen if the blank was filled in with for i in range(len(word) - 1, -1, -1) :
Question options:
The same permutations of the word would be generated in the same order
The same permutations of the word would be generated in the reverse order
The same permutations of the word would be generated in another order that is not the same as the original code segment, or the reverse of the original order
The modified code segment will not generate the same set of permutations
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
