quiz 5 Flashcards

1
Q

What is the output of the following?

def f(n):
return 2*n + f(n-1)

print(f(3))

A

f runs forever and causes StackOverflowError

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Consider a Python function f(x, y). For which data types of parameters x and y is it possible to get the same result as passing by reference?

A

dictionary and set

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The mechanism for passing parameters to a function in Python is:

A

call by assignment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the base case for the following recursive function:

def f(b, q, n):
if n == 0:
return 0
else:
return b*q**(n-1) + f(b, q, n-1)

print(f(b=2, q=3, n=4))

A

n is 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the result of the following:

def f(n):
if n==1:
return 1
else:
return 2**n*f(n-1)

print(f(3))

A

32

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Arguments to functions always appear within:

A

parentheses: ( )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What data type is used when returning multiple results from a function?

A

tuple

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the output of the following code?

def g(n, m):
if n <= 0:
return m
else:
return g(n-1, m+1)

print(g(2, 1))

A

3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What Python statement allows to suspend function execution and resume it for next call(s)?

A

yield

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

recursive functions require more computer resources and more execution time, whereas non-recursive require more lines of code

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly