Unit 4: Functions and recursion - Practice Quiz
1 Which symbol is used to call a Python function?
()
""
{}
[]
2
What is the output of print("Hello")?
Hello
print
"Hello"
3
Which statement correctly calls a function named greet?
function greet
call greet
greet[]
greet()
4
What is the result of int("8")?
8.0
8
"8"
True
5
What is the result of float(5)?
False
5
"5"
5.0
6
What is the value of 3 + 2.5 in Python?
32.5
5
5.5
6.5
7 Which function converts a number into a string?
bool()
int()
str()
float()
8 Which module provides many mathematical functions in Python?
formula
math
calculate
number
9
What does math.sqrt(25) return?
10.0
125.0
25.0
5.0
10
What does the function abs(-7) return?
-7
0
7
1
11
Which statement imports the math module?
load math
use math
include math
import math
12 Which keyword begins a function definition in Python?
fun
function
define
def
13 Which function definition is syntactically correct?
def add():
new add():
function add():
define add():
14 What keyword sends a value back from a function?
send
return
output
back
15 What is the main purpose of creating a function?
16
In def greet(name):, what is name?
17
In greet("Ana"), what is "Ana"?
18 What does a function parameter store?
19 What is recursion?
20 What does a recursive function need to stop calling itself?
21
What is printed by the following code?
print(round(abs(-4.67), 1))
4.6
5.0
4.7
-4.7
22
Given the function below, what is the value of result?
def combine(a, b):
return a * 2 + b
result = combine(combine(2, 1), 3)
15
11
13
9
23
What happens when this code is executed?
def announce():
print("Ready")
value = announce()
print(value)
None.
Ready two times.
announce has no explicit return statement.
Ready and then None.
24
What is the value and type of result after this statement?
result = int("12") + float("3.5")
123.5, a str
15, an int
15.5, a float
15.5, a str
25
Which expression evaluates to the string "Age: 21" when age = 21?
int("Age: ") + age
str("Age: " + age)
"Age: " + str(age)
"Age: " + age
26
What is printed by the following code?
x = 7
y = 2
print(float(x // y))
4.0
3
3.5
3.0
27
Which statement about the expression True + 4.5 is correct in Python?
4.5.
5.5.
5.
TypeError because Boolean values can never participate in arithmetic operations.
28
Assuming import math has been executed, which expression calculates the length of the hypotenuse for legs a and b?
math.sqrt(a**2 + b**2)
math.pow(a + b, 2)
math.sqrt(a + b)**2
math.sqrt(a**2) + b**2
29
What is printed by the following code?
import math
print(math.ceil(3.2) + math.floor(3.8))
7.0
6
7
8
30
A program stores an angle as 60 degrees. Which expression correctly computes its sine?
math.sin(60 * math.pi)
math.radians(math.sin(60))
math.sin(60)
math.sin(math.radians(60))
31 Which function correctly returns the average of three numbers?
def average(a, b, c): return a + b + c / 3
def average(a, b, c): return (a + b + c) // 2
def average(a, b, c): return (a + b + c) / 3
def average(a, b, c): print((a + b + c) / 3)
32
What is printed by this program?
def adjust(n):
n = n + 5
return n
value = 10
adjust(value)
print(value)
None
10
5
15
33
A function should return True only when a number is divisible by both 3 and 5. Which definition is correct?
def check(n): return n / 3 == 0 and n / 5 == 0
def check(n): return n % 3 == 0 or n % 5 == 0
def check(n): return n % 3 == 0 and n % 5 == 0
def check(n): return n % 15 != 0 because nonzero remainders represent divisibility
34
What is printed by the following code?
def describe(name, score=0):
print(name, score)
describe("Mira", 8)
describe("Leo")
Mira 8 followed by an error
Mira 8 followed by Leo None
Mira 0 followed by Leo 8
Mira 8 followed by Leo 0
35
Given def power(base, exponent): return base ** exponent, which call computes using keyword arguments?
power(exponent=2, 5)
power(2, base=5)
power(base=5, exponent=2)
power(exponent=5, base=2)
36
What is the result of the following call?
def calculate(x, y=2, z=3):
return x + y * z
calculate(4, z=5)
30
14
11
24
37
What happens when greet("Ana", message="Hi") is called for the function below?
def greet(message, name):
return message + ", " + name
TypeError is raised because message receives two values.
"Ana, Hi".
"Hi, Ana".
NameError is raised because keyword arguments cannot follow positional arguments in a function call.
38
What value is returned by total(4)?
def total(n):
if n == 0:
return 0
return n + total(n - 1)
10
15
6
24
39
What is the main problem with this recursive function when called with a positive integer?
def countdown(n):
print(n)
return countdown(n - 1)
40
What is returned by mystery(13)?
def mystery(n):
if n < 10:
return n
return mystery(n // 10) + n % 10
4
3
31
13
41
What is printed by the following code?
def maker():
print("M", end="")
return lambda x, y: x - y
def value(n):
print(n, end="")
return n
print(maker()(value(7), value(2)))
725M
72M5
M275
M725
42
What is printed by the following code?
def emit(x):
print(x, end="")
return x
def combine(a, b, c):
return a + b + c
print(combine(emit(1), *(emit(2),), c=emit(3)))
1263
1236
3216
123123
43
What is the output of this Python 3 expression?
print((int(-3.9), round(2.5), round(3.5), int("11", 2)))
(-4, 3, 4, 3)
(-3, 2, 3, 11)
(-3, 2, 4, 3)
(-4, 2, 4, 11)
44
What is printed by the following code?
print((bool("0"), bool(0.0), int(True), str(False)))
(False, False, 0, 'False')
(False, False, 1, 'False')
(True, False, 1, 'False')
(True, True, 1, '0')
45
What is the output of the following mixed-type operations?
print((5 // 2.0, 5 % 2.0, True + 2, 2 ** -1))
(2, 1, 3, 0)
(2.5, 0.0, 2, 0.5)
(2.0, 1.0, 3, 0.5)
(2.0, 1.0, 3.0, 0)
46
What happens when this Python 3 program is executed?
results = []
for text in ["101", "0b101", "010"]:
results.append(int(text, 0))
print(results)
ValueError on the first conversion.
ValueError on the third conversion.
[101, 5, 10].
[5, 5, 8].
47
Using the default tolerances, what is printed?
import math
print((
math.isclose(1_000_000_000, 1_000_000_001),
math.isclose(0.0, 1e-10)
))
(True, True)
(False, True)
(True, False)
(False, False)
48
Assuming standard Python binary floating-point arithmetic, what is printed?
import math
naive = (1e16 + 1.0) - 1e16
accurate = math.fsum([1e16, 1.0, -1e16])
print((naive, accurate))
(1.0, 1.0)
(0.0, 1.0)
(1.0, 0.0)
(0.0, 0.0)
49
What is the output of the following code?
import math
x = -2.7
print((math.floor(x), math.ceil(x), math.trunc(x)))
(-3, -2, -2)
(-2, -3, -2)
(-2, -2, -3)
(-3, -2, -3)
50
What happens when this annotated function is called?
def repeat(text: str, count: int) -> str:
return text * count
print(repeat(3, "ab"))
333.
TypeError before entering the function.
TypeError at the multiplication.
ababab.
51
What is printed by the following program?
def f():
return g() + 1
def g():
return 10
h = f
def g():
return 20
print(h())
20
10
11
21
52
What happens when adjust() is called?
x = 10
def adjust():
print(x)
x = 20
adjust()
10 and then sets the global value.
NameError when assigning x.
UnboundLocalError at print(x).
SyntaxError while defining the function.
53
Given the function below, which call executes without TypeError and returns 19?
def score(a, b=2, /, c=3, *, d=4):
return a + b + c + d
score(1, 5, c=6, d=7)
score(1, b=5, c=6, d=7)
score(1, 5, 6, 7)
score(a=1, b=5, c=6, d=7)
54
What happens in the following call?
def configure(a, b=0, **extra):
return a, b, extra
values = {"b": 2, "c": 3}
print(configure(1, b=4, **values))
(1, 4, {'b': 2, 'c': 3}).
TypeError because b receives two values.
(1, 2, {'c': 3}).
KeyError while unpacking values.
55
What is printed by this program?
def collect(x, items=[]):
items.append(x)
return tuple(items)
print(collect(1), collect(2), collect(3, []), collect(4))
(1,) (1, 2) (1, 2, 3) (1, 2, 3, 4)
(1,) (1, 2) (3,) (3, 4)
(1,) (2,) (3,) (4,)
(1,) (1, 2) (3,) (1, 2, 4)
56
What is printed after the following function call?
def update(a, b):
a += [9]
b = b + [8]
data = []
update(data, data)
print(data)
[9]
[]
[9, 8]
[8]
57
What value is printed by this recursive function?
def f(n):
if n == 0:
return 1
return n - f(n - 1)
print(f(4))
-1
3
1
5
58
What is printed by the following recursive traversal?
def trace(n):
if n == 0:
return
print(n, end="")
trace(n - 1)
print(n, end="")
trace(3)
321321
321123
332211
123321
59
Including the initial call, how many total invocations of counted(5) and its recursive descendants occur?
def counted(n):
if n <= 1:
return 1
return counted(n - 1) + counted(n - 2)
counted(5)
9
8
16
15
60 Consider a recursive binary search that makes exactly one recursive call on half of the remaining sorted array. For an absent target among distinct elements, what are its worst-case time complexity and auxiliary call-stack space?
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →