wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Quiz3-CC104-MakScie

Total questions: 37

Worksheet time: 37mins

Name
Class
Date
1.

def func(n):

if n == 0:

return 0

return n + func(n-1)

print(func(3))

What is the output?

a)

A. 3

b)

B. 6

c)

C. 10

d)

D. Error

2.

def rec(x):

if x <= 1:

return 1

return rec(x-1) + rec(x-1)

print(rec(3))

a)

A. 2

b)

B. 4

c)

C. 8

d)

D. 16

3.

Consider the following code:

def mystery(n):

if n == 1:

return 2

return 2 * mystery(n-1)

print(mystery(4))

a)

A. 8

b)

B. 12

c)

C. 16

d)

D. 32

4.

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)

A. 5 4 3 2 1 0

b)

B. 5 3 1

c)

C. 5 3 2 0

d)

D. 5 3 1 -1

5.

Consider the following code:

def f(n):

if n == 0:

return

print(n, end=' ')

return f(n-1)

f(3)

What is printed?

a)

A. 3 2 1

b)

B. 3 2 1 0

c)

C. 3 2

d)

D. 3

6.

def sum_digits(n):

if n == 0:

return 0

return n % 10 + sum_digits(n // 10)

print(sum_digits(321))

a)

A. 3

b)

B. 6

c)

C. 5

d)

D. 7

7.

def rec(n):

if n == 1:

return 1

return n * rec(n-2)

print(rec(5))

a)

1

b)

5

c)

10

d)

15

8.

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?

a)

number of even bits

b)

number of odd bits

c)

number of 1s in binary

d)

binary representation

9.

Consider the following Python function:

def s(n):

if n < 10:

return n

return s(n//10)

print(s(9874))

a)

A. 9

b)

B. 4

c)

C. 7

10.

def f(n):

if n == 0:

return 1

return f(n-1) + f(n-1)

print(f(3))

a)

A. 4

b)

B. 6

c)

C. 8

d)

D. 16

11.

def rec(a, b):

if b == 0:

return 1

return a * rec(a, b-1)

print(rec(2, 3))

a)

A. 6

b)

B. 8

c)

C. 4

d)

D. 9

12.

def func(n):

if n <= 0:

return

func(n-1)

print(n)

func(3)

a)

A. 1 2 3

b)

B. 3 2 1

c)

C. 2 1 3

d)

D. none

13.

def f(n):

if n == 1:

return 1

return f(n-1) + n

print(f(5))

a)

A. 12

b)

B. 13

c)

C. 15

d)

D. 10

14.

Given the following Python function:

def f(n):

if n == 1:

return 2

return 2 * f(n-1)

print(f(3))

a)

A. 8

b)

B. 12

c)

C. 14

d)

D. 16

15.

Given the following Python function:

def strange(n):

if n == 2:

return 2

return strange(n-1) * 2

print(strange(5))

a)

A. 8

b)

B. 16

c)

C. 32

d)

D. 4

16.

def g(n):

if n < 0:

return -1

return g(n-2) + 1

print(g(4))

a)

A. 3

b)

B. 2

c)

C. 5

d)

D. Error

17.

def f(n):

if n == 1:

return 0

return 1 + f(n//2)

print(f(16))

a)

3

b)

4

c)

5

d)

2

18.

def h(n):

if n == 0:

print(0)

return

h(n-1)

print(n)

h(2)

a)

A. 2 1 0

b)

B. 0 1 2

c)

C. 1 2

d)

D. 0 2 1

19.

def r(s):

if s == "":

return ""

return r(s[1:]) + s[0]

print(r("abc"))

a)

abc

b)

cba

c)

bca

20.

def m(n):

if n == 3:

return 3

return m(n+1) + 1

print(m(1))

a)

A. 3

b)

B. 4

c)

C. 5

d)

D. 2

21.

def f(n):

if n == 0:

return 0

return n%2 + f(n//2)

print(f(7))

a)

A. 2

b)

B. 3

c)

C. 1

d)

D. 0

22.

def r(n):

if n == 5:

return 5

return r(n+1) - 2

print(r(1))

a)

1

b)

-3

c)

-5

d)

-7

23.

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))

a)

1

b)

-1

c)

2

d)

0

24.

def h(n):

if n == 0:

return 1

return 3 * h(n-1)

print(h(4))

a)

27

b)

81

c)

243

d)

9

25.

def rec(n):

if n == 1:

return 1

return n + rec(n//2)

print(rec(8))

a)

10

b)

8

c)

12

d)

15

26.

def t(n):

if n < 10:

return n

return (n % 10) + t(n//10)

print(t(124))

What is the correct answer?

a)

7

b)

4

c)

1

d)

5

27.

def f(n):

if n == 2:

return 2

return f(n-2) * n

print(f(6))

a)

48

b)

36

c)

30

d)

24

28.

def p(n):

if n == 1:

return 2

return p(n-1) + 3

print(p(4))

a)

A. 8

b)

B. 11

c)

C. 12

d)

D. 14

29.

def f(n):

if n == 0: return 0

print(n)

return f(n//2)

f(10)

What is printed?

a)

A. 10 5 2 1

b)

B. 10 5 2

c)

C. 10 2 1

d)

D. 10 5 2 1 0

30.

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?

a)

hello

b)

olleh

c)

ehllo

d)

error

31.

def test(n):

if n == 1:

return 1

return test(n-1) + (n*n)

What is the output of test(3)?

a)

A. 10

b)

B. 13

c)

C. 12

d)

D. 11

32.

def f(n):

if n<=1: return 1

return f(n-1) + f(n-3)

print(f(4))

a)

3

b)

4

c)

5

d)

7

33.

def r(n):

if n==0: return 0

return n + r(n//3)

print(r(9))

a)

12

b)

13

c)

14

d)

10

34.

def f(n): if n==1: return 1 if n==2: return 2 return f(n-1)+f(n-2) print(f(5))

a)

A. 5

b)

B. 6

c)

C. 8

d)

D. 7

35.

def fun(n):

if n<1:

return 1

return n * fun(n-2)

print(fun(5))

a)

5

b)

15

c)

10

d)

20

36.

def p(n):

    if n==0: return 1

    return p(n-1) + p(n-1)

 

print(p(4))

a)

A. 8

b)

B. 16

c)

C. 32

d)

D. 64

37.

def mystery(n):

    if n == 0:

        return 0

    return 1 + mystery(n//10)

 

print(mystery(1234))

a)

A. 3

b)

B. 4

c)

C. 5

d)

D. 6