Question 1 Complete the code for the myFactorial recursive function shown below,
ID: 3830192 • Letter: Q
Question
Question 1
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 n==1:
3.return 1
4. else :
5. _______________________
Question options:
return n * myFactorial(n)
return (n - 1) * myFactorial(n)
return n * myFactorial(n - 1)
return (n - 1) * myFactorial(n - 1)
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)
This code segment would be classified as:
Question options:
backtracking
mutually recursive
non-recursive
object oriented
Question 3
Consider the following function which increments every value in a list:
def incrementList(data) :
for i in len(data) :
data[i] = data[i] + 1What is the big-Oh complexity of this algorithm, where n is the number of elements in data?
Question options:
O(1)
O(log(n))
O(n) O(n2)
Question 4
Which sort algorithm starts with an initial sequence of size 1, which is assumed to be sorted, and increases the size of the sorted sequence in the list in each iteration?
Question options:
insertion sort
selection sort
merge sort
quicksort
Question 5
Consider a list with n elements. If we visit each element n times, how many total visits will there be?
Question options
n
2n
nn
n2
Question 6
Why does the best recursive function usually run slightly slower than its iterative counterpart?
Question options:
Testing the terminating condition takes longer.
Each recursive function call takes processor time.
Multiple recursive cases must be considered.
Checking multiple terminating conditions take more processor time.
Question 7
Recursion will take place if any of the following happen:
I. function A calls function B, which calls function C
II. function A calls function B, which calls function A
III. function A calls function A Question options:
I
I and II
II
II and III
Question 8
Which of the following options could be used as a terminating condition for a recursive function that finds the middle character of a String with any number of characters?
1. I. thelengthoftheStringis1
2. II. first and last String characters match
3. III .theStringisnotempty
Question options:
I
II
I, II, and III
I and III
Explanation / Answer
Answer:
Question 1:
return n * myFactorial(n - 1)
Question 2:
mutually recursive
Question 3:
O(n)
Question 4:
insertion sort
Question 5:
n*n
Question 6:
Each recursive function call takes processor time.
Question 7:
II and III
(II is mutually recursive)
Question 8:
I (the length of the string is 1)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.