Unit 4: Functions and recursion - Practice Quiz

INT108 — Python Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which symbol is used to call a Python function?

Function calls Easy
A. Parentheses ()
B. Quotation marks ""
C. Braces {}
D. Brackets []

2 What is the output of print("Hello")?

Function calls Easy
A. Hello
B. print
C. No output
D. "Hello"

3 Which statement correctly calls a function named greet?

Function calls Easy
A. function greet
B. call greet
C. greet[]
D. greet()

4 What is the result of int("8")?

Type conversion and coercion Easy
A. The float 8.0
B. The integer 8
C. The string "8"
D. The Boolean True

5 What is the result of float(5)?

Type conversion and coercion Easy
A. The Boolean False
B. The integer 5
C. The string "5"
D. The float 5.0

6 What is the value of 3 + 2.5 in Python?

Type conversion and coercion Easy
A. 32.5
B. 5
C. 5.5
D. 6.5

7 Which function converts a number into a string?

Type conversion and coercion Easy
A. bool()
B. int()
C. str()
D. float()

8 Which module provides many mathematical functions in Python?

Math functions Easy
A. formula
B. math
C. calculate
D. number

9 What does math.sqrt(25) return?

Math functions Easy
A. 10.0
B. 125.0
C. 25.0
D. 5.0

10 What does the function abs(-7) return?

Math functions Easy
A. -7
B. 0
C. 7
D. 1

11 Which statement imports the math module?

Math functions Easy
A. load math
B. use math
C. include math
D. import math

12 Which keyword begins a function definition in Python?

Adding new functions Easy
A. fun
B. function
C. define
D. def

13 Which function definition is syntactically correct?

Adding new functions Easy
A. def add():
B. new add():
C. function add():
D. define add():

14 What keyword sends a value back from a function?

Adding new functions Easy
A. send
B. return
C. output
D. back

15 What is the main purpose of creating a function?

Adding new functions Easy
A. To change code into comments
B. To reuse a block of code
C. To stop the Python interpreter
D. To remove all variables

16 In def greet(name):, what is name?

Parameters and arguments Easy
A. A return value
B. A module
C. An argument
D. A parameter

17 In greet("Ana"), what is "Ana"?

Parameters and arguments Easy
A. A function definition
B. A parameter
C. An argument
D. A keyword

18 What does a function parameter store?

Parameters and arguments Easy
A. The name of the Python program
B. A list of modules
C. A value passed to the function
D. The computer's current time

19 What is recursion?

Recursion and its use Easy
A. A function calling itself
B. A module being imported
C. A variable changing type
D. A loop with no condition

20 What does a recursive function need to stop calling itself?

Recursion and its use Easy
A. A base case
B. A new module
C. A print statement
D. A type conversion

21 What is printed by the following code?

print(round(abs(-4.67), 1))

Function calls Medium
A. 4.6
B. 5.0
C. 4.7
D. -4.7

22 Given the function below, what is the value of result?

PYTHON
def combine(a, b):
    return a * 2 + b

result = combine(combine(2, 1), 3)

Function calls Medium
A. 15
B. 11
C. 13
D. 9

23 What happens when this code is executed?

PYTHON
def announce():
    print("Ready")

value = announce()
print(value)

Function calls Medium
A. It prints only None.
B. It prints Ready two times.
C. It raises an error because announce has no explicit return statement.
D. It prints Ready and then None.

24 What is the value and type of result after this statement?

result = int("12") + float("3.5")

Type conversion and coercion Medium
A. 123.5, a str
B. 15, an int
C. 15.5, a float
D. 15.5, a str

25 Which expression evaluates to the string "Age: 21" when age = 21?

Type conversion and coercion Medium
A. int("Age: ") + age
B. str("Age: " + age)
C. "Age: " + str(age)
D. "Age: " + age

26 What is printed by the following code?

PYTHON
x = 7
y = 2
print(float(x // y))

Type conversion and coercion Medium
A. 4.0
B. 3
C. 3.5
D. 3.0

27 Which statement about the expression True + 4.5 is correct in Python?

Type conversion and coercion Medium
A. It evaluates to 4.5.
B. It evaluates to 5.5.
C. It evaluates to 5.
D. It raises a 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 functions Medium
A. math.sqrt(a**2 + b**2)
B. math.pow(a + b, 2)
C. math.sqrt(a + b)**2
D. math.sqrt(a**2) + b**2

29 What is printed by the following code?

PYTHON
import math
print(math.ceil(3.2) + math.floor(3.8))

Math functions Medium
A. 7.0
B. 6
C. 7
D. 8

30 A program stores an angle as 60 degrees. Which expression correctly computes its sine?

Math functions Medium
A. math.sin(60 * math.pi)
B. math.radians(math.sin(60))
C. math.sin(60)
D. math.sin(math.radians(60))

31 Which function correctly returns the average of three numbers?

Adding new functions Medium
A. def average(a, b, c): return a + b + c / 3
B. def average(a, b, c): return (a + b + c) // 2
C. def average(a, b, c): return (a + b + c) / 3
D. def average(a, b, c): print((a + b + c) / 3)

32 What is printed by this program?

PYTHON
def adjust(n):
    n = n + 5
    return n

value = 10
adjust(value)
print(value)

Adding new functions Medium
A. None
B. 10
C. 5
D. 15

33 A function should return True only when a number is divisible by both 3 and 5. Which definition is correct?

Adding new functions Medium
A. def check(n): return n / 3 == 0 and n / 5 == 0
B. def check(n): return n % 3 == 0 or n % 5 == 0
C. def check(n): return n % 3 == 0 and n % 5 == 0
D. def check(n): return n % 15 != 0 because nonzero remainders represent divisibility

34 What is printed by the following code?

PYTHON
def describe(name, score=0):
    print(name, score)

describe("Mira", 8)
describe("Leo")

Parameters and arguments Medium
A. Mira 8 followed by an error
B. Mira 8 followed by Leo None
C. Mira 0 followed by Leo 8
D. Mira 8 followed by Leo 0

35 Given def power(base, exponent): return base ** exponent, which call computes using keyword arguments?

Parameters and arguments Medium
A. power(exponent=2, 5)
B. power(2, base=5)
C. power(base=5, exponent=2)
D. power(exponent=5, base=2)

36 What is the result of the following call?

PYTHON
def calculate(x, y=2, z=3):
    return x + y * z

calculate(4, z=5)

Parameters and arguments Medium
A. 30
B. 14
C. 11
D. 24

37 What happens when greet("Ana", message="Hi") is called for the function below?

PYTHON
def greet(message, name):
    return message + ", " + name

Parameters and arguments Medium
A. A TypeError is raised because message receives two values.
B. The function returns "Ana, Hi".
C. The function returns "Hi, Ana".
D. A NameError is raised because keyword arguments cannot follow positional arguments in a function call.

38 What value is returned by total(4)?

PYTHON
def total(n):
    if n == 0:
        return 0
    return n + total(n - 1)

Recursion and its use Medium
A. 10
B. 15
C. 6
D. 24

39 What is the main problem with this recursive function when called with a positive integer?

PYTHON
def countdown(n):
    print(n)
    return countdown(n - 1)

Recursion and its use Medium
A. It returns the printed values as a list only after all recursive calls have completed.
B. It has no base case to stop the recursive calls.
C. It subtracts one instead of adding one.
D. It must use a loop before making the recursive call.

40 What is returned by mystery(13)?

PYTHON
def mystery(n):
    if n < 10:
        return n
    return mystery(n // 10) + n % 10

Recursion and its use Medium
A. 4
B. 3
C. 31
D. 13

41 What is printed by the following code?

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

Function calls Hard
A. 725M
B. 72M5
C. M275
D. M725

42 What is printed by the following code?

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

Function calls Hard
A. 1263
B. 1236
C. 3216
D. 123123

43 What is the output of this Python 3 expression?

PYTHON
print((int(-3.9), round(2.5), round(3.5), int("11", 2)))

Type conversion and coercion Hard
A. (-4, 3, 4, 3)
B. (-3, 2, 3, 11)
C. (-3, 2, 4, 3)
D. (-4, 2, 4, 11)

44 What is printed by the following code?

PYTHON
print((bool("0"), bool(0.0), int(True), str(False)))

Type conversion and coercion Hard
A. (False, False, 0, 'False')
B. (False, False, 1, 'False')
C. (True, False, 1, 'False')
D. (True, True, 1, '0')

45 What is the output of the following mixed-type operations?

PYTHON
print((5 // 2.0, 5 % 2.0, True + 2, 2 ** -1))

Type conversion and coercion Hard
A. (2, 1, 3, 0)
B. (2.5, 0.0, 2, 0.5)
C. (2.0, 1.0, 3, 0.5)
D. (2.0, 1.0, 3.0, 0)

46 What happens when this Python 3 program is executed?

PYTHON
results = []
for text in ["101", "0b101", "010"]:
    results.append(int(text, 0))
print(results)

Type conversion and coercion Hard
A. It raises ValueError on the first conversion.
B. It raises ValueError on the third conversion.
C. It prints [101, 5, 10].
D. It prints [5, 5, 8].

47 Using the default tolerances, what is printed?

PYTHON
import math

print((
    math.isclose(1_000_000_000, 1_000_000_001),
    math.isclose(0.0, 1e-10)
))

Math functions Hard
A. (True, True)
B. (False, True)
C. (True, False)
D. (False, False)

48 Assuming standard Python binary floating-point arithmetic, what is printed?

PYTHON
import math

naive = (1e16 + 1.0) - 1e16
accurate = math.fsum([1e16, 1.0, -1e16])
print((naive, accurate))

Math functions Hard
A. (1.0, 1.0)
B. (0.0, 1.0)
C. (1.0, 0.0)
D. (0.0, 0.0)

49 What is the output of the following code?

PYTHON
import math

x = -2.7
print((math.floor(x), math.ceil(x), math.trunc(x)))

Math functions Hard
A. (-3, -2, -2)
B. (-2, -3, -2)
C. (-2, -2, -3)
D. (-3, -2, -3)

50 What happens when this annotated function is called?

PYTHON
def repeat(text: str, count: int) -> str:
    return text * count

print(repeat(3, "ab"))

Adding new functions Hard
A. It prints 333.
B. It raises TypeError before entering the function.
C. It raises TypeError at the multiplication.
D. It prints ababab.

51 What is printed by the following program?

PYTHON
def f():
    return g() + 1

def g():
    return 10

h = f

def g():
    return 20

print(h())

Adding new functions Hard
A. 20
B. 10
C. 11
D. 21

52 What happens when adjust() is called?

PYTHON
x = 10

def adjust():
    print(x)
    x = 20

adjust()

Adding new functions Hard
A. It prints 10 and then sets the global value.
B. It raises NameError when assigning x.
C. It raises UnboundLocalError at print(x).
D. It raises SyntaxError while defining the function.

53 Given the function below, which call executes without TypeError and returns 19?

PYTHON
def score(a, b=2, /, c=3, *, d=4):
    return a + b + c + d

Parameters and arguments Hard
A. score(1, 5, c=6, d=7)
B. score(1, b=5, c=6, d=7)
C. score(1, 5, 6, 7)
D. score(a=1, b=5, c=6, d=7)

54 What happens in the following call?

PYTHON
def configure(a, b=0, **extra):
    return a, b, extra

values = {"b": 2, "c": 3}
print(configure(1, b=4, **values))

Parameters and arguments Hard
A. It returns (1, 4, {'b': 2, 'c': 3}).
B. It raises TypeError because b receives two values.
C. It returns (1, 2, {'c': 3}).
D. It raises KeyError while unpacking values.

55 What is printed by this program?

PYTHON
def collect(x, items=[]):
    items.append(x)
    return tuple(items)

print(collect(1), collect(2), collect(3, []), collect(4))

Parameters and arguments Hard
A. (1,) (1, 2) (1, 2, 3) (1, 2, 3, 4)
B. (1,) (1, 2) (3,) (3, 4)
C. (1,) (2,) (3,) (4,)
D. (1,) (1, 2) (3,) (1, 2, 4)

56 What is printed after the following function call?

PYTHON
def update(a, b):
    a += [9]
    b = b + [8]

data = []
update(data, data)
print(data)

Parameters and arguments Hard
A. [9]
B. []
C. [9, 8]
D. [8]

57 What value is printed by this recursive function?

PYTHON
def f(n):
    if n == 0:
        return 1
    return n - f(n - 1)

print(f(4))

Recursion and its use Hard
A. -1
B. 3
C. 1
D. 5

58 What is printed by the following recursive traversal?

PYTHON
def trace(n):
    if n == 0:
        return
    print(n, end="")
    trace(n - 1)
    print(n, end="")

trace(3)

Recursion and its use Hard
A. 321321
B. 321123
C. 332211
D. 123321

59 Including the initial call, how many total invocations of counted(5) and its recursive descendants occur?

PYTHON
def counted(n):
    if n <= 1:
        return 1
    return counted(n - 1) + counted(n - 2)

counted(5)

Recursion and its use Hard
A. 9
B. 8
C. 16
D. 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?

Recursion and its use Hard
A. Time and space
B. Time and space
C. Time and space
D. Time and space