Unit 2: Control Flow, Functions, and Problem-Solving - Practice Quiz
1
What does the else block execute when the if condition is false?
if block
else block
2 Which loop is commonly used to repeat through each item in a sequence?
for loop
if loop
def loop
try loop
3 What is a nested loop?
4
What does the break statement do inside a loop?
5
What value does this code print? count = 0; count += 1; print(count)
6
What numbers are produced by range(3)?
7 Which keyword is used to define a function in Python?
def
define
func
function
8
In greet("Sam"), what is "Sam" called?
9 Which keyword sends a value back from a function?
give
return
output
send
10 What is recursion in programming?
11 Which keyword creates a small anonymous function in Python?
quick
lambda
anonymous
small
12
What is the result of "Python"[0:2]?
Pyt
ho
Py
Python
13 Which syntax is an f-string in Python?
string("Hello {name}")
"Hello {name}"
f"Hello {name}"
format "Hello {name}"
14
What does the string method .upper() do?
15
What does "a,b,c".split(",") return?
("a", "b", "c")
"abc"
["a", "b", "c"]
16
What is the result of "Hello, {}".format("Mia")?
Mia, Hello
Hello, format
Hello, Mia
Hello, {Mia}
17 What is a regular expression mainly used for?
18 What is an algorithm?
19 Which pattern is commonly used to find the total of numbers in a list?
20
What does the continue statement do inside a loop?
21
What is printed by this code?
score = 72
if score >= 90:
grade = "A"
elif score >= 70:
grade = "B"
else:
grade = "C"
print(grade)
22
What is the final value of total?
total = 0
number = 1
while number <= 4:
total += number
number += 1
23
How many times is count += 1 executed?
count = 0
for i in range(3):
for j in range(2):
count += 1
24
What is printed by this code?
values = [1, 2, 3, 4, 5]
for value in values:
if value == 3:
continue
if value == 5:
break
print(value, end=" ")
25
What is the value of count after execution?
text = "banana"
count = 0
for character in text:
if character == "a":
count += 1
26
What list is produced by list(range(2, 10, 3))?
27
What does the following function return when called as square(5)?
def square(number):
return number * number
28
What is printed by this code?
def describe(name, age=18):
print(name, age)
describe("Mina", 21)
describe("Leo")
29
What is printed by this code?
def add_tax(price):
price *= 1.1
result = add_tax(100)
print(result)
30
What is returned by sum_down(4)?
def sum_down(n):
if n == 0:
return 0
return n + sum_down(n - 1)
31
What is the result of this expression?
numbers = [1, 2, 3, 4]
result = list(map(lambda x: x * 2 + 1, numbers))
32
What is the value of result?
word = "PYTHON"
result = word[1:5:2]
33
What is printed by this code?
name = "Ava"
score = 87.456
print(f"{name}: {score:.1f}")
34
What is the value of cleaned?
text = " Python Programming "
cleaned = text.strip().lower().replace(" ", "-")
35
What is the result of this code?
text = "red,green,blue"
colors = text.split(",")
result = " | ".join(colors)
36
What is printed by this code?
item = "book"
price = 12.5
print("{} costs ${:.2f}".format(item, price))
37
Which pattern matches a string containing exactly three digits?
import re
r"\\d+"
r"^\\d{3}$"
r"[0-9]"
r"\\w{3}"
38
Which strategy correctly finds the largest value in a nonempty list without using max()?
39
Which code correctly counts the number of even values in numbers?
count = 0
for n in numbers:
if n % 2 == 0:
count += 1count = 1
for n in numbers:
if n / 2 == 0:
count += 1count = 0
for n in numbers:
if n % 2 != 0:
count -= 1count = 0
for n in numbers:
if n % 2 == 1:
count += 1
40
Which code correctly creates a list containing the squares of the positive values in numbers?
squares = [n ** 2 for n in numbers if n == 0]squares = [n * n for n in numbers if n < 0]squares = [n + n for n in numbers if n > 0]squares = [n * n for n in numbers if n > 0]
41
What is printed by this code? x, y = 8, 3\nif x % y == 0:\n result = "A"\nelif x // y == 2 and x > y:\n result = "B"\nelse:\n result = "C"\nprint(result)
ZeroDivisionError
42
What list is produced by list(range(10, -2, -3))?
[10, 7, 4, 1]
[10, 8, 6, 4, 2, 0]
[10, 7, 4, 1, -2]
[7, 4, 1, -2]
43
What is printed by this code? total = 0\nfor i in range(1, 4):\n for j in range(i, 4):\n total += i * j\nprint(total)
44
What is printed? values = []\nfor n in range(2, 10):\n if n % 2 == 0:\n continue\n if n > 6:\n break\n values.append(n)\nprint(values)
[2, 4, 6]
[3, 5]
[2, 3, 4, 5, 6]
[3, 5, 7]
45
Which expression correctly counts the number of overlapping occurrences of "ana" in "banana"?
"banana".count("ana")
len("banana".split("ana")) - 1
sum("banana"[i:i+3] == "ana" for i in range(len("banana") - 2))
sum("ana" in "banana"[i:] for i in range(len("banana")))
46
What is printed by this code? def update(items=[]):\n items.append(len(items))\n return items\n\nprint(update())\nprint(update())
TypeError occurs because the default list is immutable
[0] followed by [1]
[0] followed by [0]
[1] followed by [1]
47
What happens when this code runs? def f(a, b=2, *args, **kwargs):\n return a + b + len(args) + len(kwargs)\n\nprint(f(5, 3, 7, 8, x=1))
12
10
13
TypeError because positional arguments follow a default argument
48
What is printed by this code? def transform(x):\n if x < 0:\n return\n return x * 2\n\nprint(transform(-1), transform(4))
ValueError for the negative argument
None 8
None None
0 8
49
What is printed by this function call? def f(n):\n if n <= 1:\n return 1\n return n * f(n - 2)\n\nprint(f(6))
50
What is the result of sorted([(1, 4), (2, 1), (1, 2)], key=lambda p: (p[0], -p[1]))?
[(1, 2), (1, 4), (2, 1)]
[(1, 4), (1, 2), (2, 1)]
[(1, 4), (2, 1), (1, 2)]
[(2, 1), (1, 4), (1, 2)]
51
What is printed by s = "algorithm"; print(s[-2:1:-2])?
mto
mrt
hio
mgt
52
What is printed by x = 12.3456; print(f"{x:08.2f}")?
0012.346
00012.35
12.345600
12.35
53
What is the result of " A\tB\n".strip().replace("\t", "-").lower()?
" a-b "
"A-B"
"a-b"
"a\\tb"
54
What is printed by text = "a,,b,"; print("|".join(text.split(",")))?
a,,b,
a||b|
a| |b|
a|b
55
What is printed by template = "{1}-{0}:{1}"; print(template.format("x", "y"))?
x-y:x
KeyError is raised because numeric fields are invalid
x-y:y
y-x:y
56
Using Python's re.findall, what does re.findall(r"\b\w+\b", "hi, 42 times!") return?
['h', 'i', '4', '2', 't', 'i', 'm', 'e', 's']
['hi', '42', 'times']
['hi', 'times']
['hi,', '42', 'times!']
57 A sorted list may contain duplicates. Which strategy finds whether two distinct elements sum to a target in time and extra space?
58
Which code correctly returns the first character in s whose frequency is exactly one, or None if no such character exists?
return next((c for c in set(s) if s.count(c) == 1), None)
return min((c for c in s if s.count(c) == 1), default=None)
return next((c for c in s if s.count(c) == 1), None)
return next((c for c in s if s.count(c) > 1), None)
59
What is printed by this code? n = 19\nsteps = 0\nwhile n > 1:\n n = n // 2\n steps += 1\nprint(n, steps)
1 5
1 4
2 4
0 4
60
What does pal("racecar", 0, 6) return? def pal(s, left, right):\n if left >= right:\n return True\n return s[left] == s[right] and pal(s, left + 1, right - 1)
None
False
True
IndexError at the recursive base case
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 →