NEW
Font size
WorksheetsQuiz3-CC104-MakScie
Total questions: 37
Worksheet time: 37mins
def func(n):
if n == 0:
return 0
return n + func(n-1)
print(func(3))
What is the output?
A. 3
B. 6
C. 10
D. Error
def rec(x):
if x <= 1:
return 1
return rec(x-1) + rec(x-1)
print(rec(3))
A. 2
B. 4
C. 8
D. 16
Consider the following code:
def mystery(n):
if n == 1:
return 2
return 2 * mystery(n-1)
print(mystery(4))
A. 8
B. 12
C. 16
D. 32
Consider the following code:
def count_down(n):
if n < 0:
return
print(n)
count_down(n-2)
count_down(5)
What is printed?
A. 5 4 3 2 1 0
B. 5 3 1
C. 5 3 2 0
D. 5 3 1 -1
Consider the following code:
def f(n):
if n == 0:
return
print(n, end=' ')
return f(n-1)
f(3)
What is printed?
A. 3 2 1
B. 3 2 1 0
C. 3 2
D. 3
def sum_digits(n):
if n == 0:
return 0
return n % 10 + sum_digits(n // 10)
print(sum_digits(321))
A. 3
B. 6
C. 5
D. 7
def rec(n):
if n == 1:
return 1
return n * rec(n-2)
print(rec(5))
1
5
10
15
Consider the following Python function:
def r(n):
if n == 0:
return 0
return n % 2 + r(n//2)
print(r(10))
What does this function compute?
number of even bits
number of odd bits
number of 1s in binary
binary representation
Consider the following Python function:
def s(n):
if n < 10:
return n
return s(n//10)
print(s(9874))
A. 9
B. 4
C. 7
def f(n):
if n == 0:
return 1
return f(n-1) + f(n-1)
print(f(3))
A. 4
B. 6
C. 8
D. 16
def rec(a, b):
if b == 0:
return 1
return a * rec(a, b-1)
print(rec(2, 3))
A. 6
B. 8
C. 4
D. 9
def func(n):
if n <= 0:
return
func(n-1)
print(n)
func(3)
A. 1 2 3
B. 3 2 1
C. 2 1 3
D. none
def f(n):
if n == 1:
return 1
return f(n-1) + n
print(f(5))
A. 12
B. 13
C. 15
D. 10
Given the following Python function:
def f(n):
if n == 1:
return 2
return 2 * f(n-1)
print(f(3))
A. 8
B. 12
C. 14
D. 16
Given the following Python function:
def strange(n):
if n == 2:
return 2
return strange(n-1) * 2
print(strange(5))
A. 8
B. 16
C. 32
D. 4
def g(n):
if n < 0:
return -1
return g(n-2) + 1
print(g(4))
A. 3
B. 2
C. 5
D. Error
def f(n):
if n == 1:
return 0
return 1 + f(n//2)
print(f(16))
3
4
5
2
def h(n):
if n == 0:
print(0)
return
h(n-1)
print(n)
h(2)
A. 2 1 0
B. 0 1 2
C. 1 2
D. 0 2 1
def r(s):
if s == "":
return ""
return r(s[1:]) + s[0]
print(r("abc"))
abc
cba
bca
def m(n):
if n == 3:
return 3
return m(n+1) + 1
print(m(1))
A. 3
B. 4
C. 5
D. 2
def f(n):
if n == 0:
return 0
return n%2 + f(n//2)
print(f(7))
A. 2
B. 3
C. 1
D. 0
def r(n):
if n == 5:
return 5
return r(n+1) - 2
print(r(1))
1
-3
-5
-7
Given the following Python function:
def f(n):
if n == 0:
return 1
if n == 1:
return -1
return f(n-1) - f(n-2)
print(f(5))
1
-1
2
0
def h(n):
if n == 0:
return 1
return 3 * h(n-1)
print(h(4))
27
81
243
9
def rec(n):
if n == 1:
return 1
return n + rec(n//2)
print(rec(8))
10
8
12
15
def t(n):
if n < 10:
return n
return (n % 10) + t(n//10)
print(t(124))
What is the correct answer?
7
4
1
5
def f(n):
if n == 2:
return 2
return f(n-2) * n
print(f(6))
48
36
30
24
def p(n):
if n == 1:
return 2
return p(n-1) + 3
print(p(4))
A. 8
B. 11
C. 12
D. 14
def f(n):
if n == 0: return 0
print(n)
return f(n//2)
f(10)
What is printed?
A. 10 5 2 1
B. 10 5 2
C. 10 2 1
D. 10 5 2 1 0
Consider the following code:
if len(s)==1:
return s
return s[-1] + rec(s[:-1])
print(rec("hello"))
What is the output of the code above?
hello
olleh
ehllo
error
def test(n):
if n == 1:
return 1
return test(n-1) + (n*n)
What is the output of test(3)?
A. 10
B. 13
C. 12
D. 11
def f(n):
if n<=1: return 1
return f(n-1) + f(n-3)
print(f(4))
3
4
5
7
def r(n):
if n==0: return 0
return n + r(n//3)
print(r(9))
12
13
14
10
def f(n): if n==1: return 1 if n==2: return 2 return f(n-1)+f(n-2) print(f(5))
A. 5
B. 6
C. 8
D. 7
def fun(n):
if n<1:
return 1
return n * fun(n-2)
print(fun(5))
5
15
10
20
def p(n):
if n==0: return 1
return p(n-1) + p(n-1)
print(p(4))
A. 8
B. 16
C. 32
D. 64
def mystery(n):
if n == 0:
return 0
return 1 + mystery(n//10)
print(mystery(1234))
A. 3
B. 4
C. 5
D. 6
