Unit 1: Python basics - Practice Quiz
1 Which file extension is commonly used for Python source files?
2 Which function displays output on the screen in Python?
input()
print()
type()
len()
3 Which symbol begins a single-line comment in Python?
#
--
/*
//
4 Which function accepts input from the user in Python?
print()
input()
range()
open()
5 Which of the following is a valid Python variable name?
2score
class
user-score
user_score
6 Which Python data type stores whole numbers?
bool
str
int
float
7
What is the data type of the value 3.14 in Python?
str
bool
int
float
8 Which operator is used for exponentiation in Python?
^
%
**
//
9
What is the result of 10 % 3?
3
3.33
0
1
10 Which operator checks whether two values are equal?
!=
>=
=
==
11 Which keyword begins a conditional statement in Python?
return
if
def
for
12
Which keyword provides an alternative when an if condition is false?
elif
break
else
continue
13 Which loop is commonly used to iterate over a sequence in Python?
for loop
def loop
else loop
if loop
14 Which statement immediately exits the nearest loop?
break
pass
return
continue
15 Which statement skips the rest of the current loop iteration?
break
else
pass
continue
16 Which keyword is used to define a function in Python?
func
def
function
define
17
What is the purpose of the return statement in a function?
18
In def greet(name):, what is name?
19
How do you call a function named show that takes no arguments?
show()
show
call show
def show
20 Which built-in function returns the number of items in a list?
type()
input()
len()
range()
21
What is printed by the following code?
x = 10
x = "10"
print(x * 2)
10 10
TypeError
20
1010
22
A file named tools.py contains:
print("loaded")
if __name__ == "__main__":
print("running")
What is printed when another program executes import tools?
running
loaded
loaded then running
23
What is the output of this simultaneous assignment?
a, b = 2, 3
a, b = b, a + b
print(a, b)
3 6
5 3
2 5
3 5
24
What does this program print?
total = 4
Total = 7
TOTAL = total + Total
print(TOTAL)
7
NameError
8
11
25
What is printed by print(type((5,)).__name__, type((5)).__name__)?
int tuple
int int
tuple int
tuple tuple
26
What is the output of print(-7 // 3, -7 % 3)?
-2 1
-3 2
-3 -1
-2 -1
27
What is printed by the following code?
a = [1, 2]
b = a
c = a[:]
b.append(3)
print(c, a)
[1, 2] [1, 2, 3]
[1, 2, 3] [1, 2, 3]
[1, 2] [1, 2]
[1, 2, 3] [1, 2]
28
What is the output of this dictionary operation?
d = {"a": 1, "b": 2, "a": 3}
print(len(d), d["a"])
3 3
3 1
2 1
2 3
29
Which set is produced by {1, 2, 3} & {2, 3, 4} | {4, 5}?
{2, 3}
{2, 3, 4, 5}
{1, 2, 3, 4}
{1, 2, 3, 4, 5}
30
What value is printed by print(-2 ** 2 + 2 ** 3)?
-4
-12
4
12
31
What is printed by the following loop?
total = 0
for n in range(2, 10, 3):
total += n
print(total)
12
20
15
18
32
Which letter is printed by this conditional?
x = 8
if x % 2 == 0 and x > 10:
print("A")
elif x % 2 == 0 or x > 10:
print("B")
else:
print("C")
A
C
B
33
What value is printed after this loop?
n = 20
count = 0
while n > 1:
n //= 2
count += 1
print(count)
10
5
3
4
34
What is the final value of result?
result = []
for n in range(2, 10):
if n % 3 == 0:
continue
if n > 7:
break
result.append(n)
[2, 4, 5, 7]
[2, 4, 5, 7, 8, 9]
[2, 3, 4, 5, 7]
[2, 4, 5, 7, 8]
35
What is printed by these nested loops?
count = 0
for i in range(1, 4):
for j in range(1, 4):
if i == j:
continue
count += 1
print(count)
8
9
3
6
36
What is printed by the following code?
def add_item(item, items=[]):
items.append(item)
return items
print(add_item(1))
print(add_item(2))
[1, 2] then [1, 2]
[1] then [2]
[1] then [1, 2]
[1, 2] then [2]
37
What is the output of this program?
x = 10
def change():
x = 3
return x
print(change(), x)
3 3
10 10
3 10
10 3
38
What value is returned by calculate(3, 4, 5, 6)?
def calculate(a, b=2, *numbers):
return a + b + sum(numbers)
12
18
14
20
39
What is printed by this function call?
def divide_parts(n):
return n // 2, n % 2
q, r = divide_parts(7)
print(q, r)
2 3
3 1
4 1
3 2
40
What is the value of ordered after this statement?
words = ["pear", "fig", "apple", "kiwi"]
ordered = sorted(words, key=lambda word: (len(word), word))
["fig", "pear", "kiwi", "apple"]
["fig", "kiwi", "pear", "apple"]
["pear", "fig", "apple", "kiwi"]
["apple", "kiwi", "pear", "fig"]
41
What is printed by the following code?
a = [0, 0]
i = 0
i, a[i] = 1, 7
print(a, i)
[7, 0] 0
[0, 7] 0
[7, 0] 1
[0, 7] 1
42
Assuming no future imports, what is printed?
def f(x: int = '3') -> int:
return x * 2
print(f(), f(4))
TypeError
33 8
33 44
6 8
43
What happens when this code is executed?
x = 10
def f(flag):
if flag:
x = 20
return x
print(f(False))
NameError
UnboundLocalError
10
20
44
In Python 3.10 or later, what is printed by this module-level code?
x = 100
s = [x := x + i for i in range(3)]
print(x, s, 'i' in globals())
102 [100, 101, 102] False
103 [100, 101, 103] False
103 [100, 101, 103] True
100 [100, 101, 103] True
45
A file named m.py contains only print(__name__). What does it print when run directly, and what does it print when first imported as m by another script?
__main__; imported: m.py
m.py; imported: m
m; imported: __main__
__main__; imported: m
46
What is printed by the following code?
x = [1, 2]
y = x
x += [3]
z = x
x = x + [4]
print(y, z, x, y is z, z is x)
[1, 2] [1, 2] [1, 2, 3, 4] True False
[1, 2, 3] [1, 2, 3] [1, 2, 3, 4] True False
[1, 2] [1, 2, 3] [1, 2, 3, 4] False False
[1, 2, 3, 4] [1, 2, 3, 4] [1, 2, 3, 4] True True
47
What is printed?
result = 0 or [] or 'py' and 5
print(result, type(result).__name__)
[] list
5 int
py str
False bool
48
What is printed by this dictionary code?
d = {True: 'A', 1: 'B', 1.0: 'C'}
print(len(d), list(d.keys()), d[1])
1 [True] C
1 [1.0] C
2 [True, 1.0] B
3 [True, 1, 1.0] C
49
What is printed by these slices?
a = [0, 1, 2, 3, 4, 5]
print(a[5:0:-2], a[::-2])
[5, 3] [5, 3, 1]
[5, 3, 1] [5, 3, 1]
[5, 3, 1] [4, 2, 0]
[4, 2] [5, 3, 1]
50
What exact text is printed?
def f(v):
print(v, end='')
return v
print(f(3) < f(2) < f(1))
321False
32True
321True
32False
51
What exact text is printed?
for i in range(3):
try:
if i == 1:
continue
print(i, end='')
finally:
print('F', end='')
0F1F2F
0FF2F
01FF2F
0F2F
52
What is printed?
n = 3
while n:
n -= 1
if n == 1:
break
else:
print('E', end='')
print(n)
E1
E0
0
1
53
What exact text is printed?
for i in range(4):
for j in range(4):
if i * j > 2:
break
else:
print(i, end='')
01
0123
0
54
What is printed by the following code?
def g(x):
try:
r = 10 // x
except ZeroDivisionError:
r = 0
else:
r += 1
finally:
r *= 2
return r
print(g(2), g(0))
10 0
12 0
5 0
12 2
55
What is printed by this comprehension?
print([i for i in range(6) if i % 2 if i % 3])
[1, 5]
[0, 2, 4]
[1, 3, 5]
[2, 4]
56
What is printed?
def f(x, acc=[]):
acc += [x]
return acc
a = f(1)
b = f(2, [])
c = f(3)
print(a, b, c, a is c)
[1] [2] [1, 3] False
[1, 3] [2, 3] [1, 3] True
[1, 3] [2] [1, 3] True
[1] [2] [3] False
57
What is printed by these functions?
funcs = []
for i in range(3):
funcs.append(lambda x=i: x + i)
i = 10
print([f() for f in funcs])
[12, 12, 12]
[20, 20, 20]
[10, 11, 12]
[0, 2, 4]
58
In Python 3.10 or later, which call raises TypeError?
def f(a, /, b=2, *, c=3):
return a + b + c
f(1, c=3)
f(1, 2, c=3)
f(1, b=2, c=3)
f(a=1, b=2, c=3)
59
What is printed after applying these decorators?
def deco(tag):
def outer(fn):
def inner():
return tag + fn() + tag
return inner
return outer
@deco('A')
@deco('B')
def f():
return 'X'
print(f())
AABXB
ABXBA
BBAAX
BAXAB
60
What is printed by this generator code?
def gen():
yield 1
return 7
g = gen()
a = next(g)
try:
next(g)
except StopIteration as e:
b = e.value
print(a, b)
StopIteration
1 7
1 None
1 0
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 →