Unit 1: Setting up your Programming Environment; Variables, Expression and Statements - Practice Quiz
1 Which major Python version is recommended for writing modern Python programs?
2 Which command commonly checks the installed Python version in Windows Command Prompt?
3
Which Python statement correctly displays Hello, World!?
4
Which statement correctly assigns the value 25 to a variable named age?
5 What should you do before using a variable in a Python expression?
6
What is the Python type of the value 42?
bool
int
float
str
7
What is the Python type of the value 3.14?
bool
str
float
int
8 What is a variable in Python?
9 Which of the following is a valid Python variable name?
10
Why should Python keywords such as if and class not be used as variable names?
11 Which line is an assignment statement in Python?
12
In the expression 8 + 2, which symbol is the operator?
2
10
8
+
13 Which Python operator performs multiplication?
-
/
+
*
14
What is the result of 2 + 3 * 4 in Python?
11
14
20
24
15
What is the result of (2 + 3) * 4 in Python?
14
18
24
20
16
What is the result of "Py" + "thon"?
"Py+thon"
"Python"
"thonPy"
"Py thon"
17
What is the result of "Hi" * 3?
"HHHiii"
"HiHiHi"
"Hi3"
"Hi Hi Hi"
18
Which statement uses an expression as an argument to print()?
19 Which symbol begins a single-line comment in Python?
//
**
<!--
#
20 What is the main purpose of comments in a Python program?
21
In Python 3, what is the result of the expression 5 / 2?
2.0
2.5
3
2
22
A Python 3 script named calculate.py is saved in the current Windows directory. Which command commonly runs it using the Python launcher?
python3.start calculate.py
py -3 calculate.py
execute -py calculate.py
run calculate.py
23
A file named hello.py contains print('Hello, world!'). Which command runs it from a terminal when Python is available on the system path?
python hello.py
open hello.py
start-python hello.py
runfile hello.py
24
What is printed by this code? price = 8; quantity = 3; total = price * quantity; print(total)
24
11
8 * 3
83
25
Which revision avoids a NameError in the following code? print(message) followed by message = 'Ready'
message to a comment
print(message) below the assignment
message with Message
message
26
What type does type(7.0) report in Python?
decimal
number
float
int
27
What is printed by this code? score = 10; score = score + 5; print(score)
15
105
score + 5
10
28
Which statement correctly assigns the value 25 to a valid Python variable?
class = 25
2nd_place = 25
total-score = 25
total_score = 25
29
How many Python statements are executed by the following code? x = 4; y = 6; print(x + y)
30
In the expression result = 12 - 5, which items are the operands of the subtraction operator?
- and =
result and =
12 and 5
result and 12
31
What is the value of 3 + 4 * 2 ** 2?
19
28
49
64
32
What is printed by this code? word = 'Py'; print(word * 3)
Py Py Py
Py*Py*Py
PyPyPy
Py3
33
What is the result of 'data' + 'base' in Python?
+
data+base
data base
database
34
What is printed by this code? name = 'Mina'; greeting = 'Hi, ' + name + '!'; print(greeting)
Hi, name!
Hi, + Mina + !
Hi, Mina!
'Hi, Mina!'
35
What is printed by the following code? x = 4 # initial value followed by print(x + 1)
4
5
initial value
36 Which statement best describes why Python version selection matters when running a program?
37
A Windows terminal reports that python is not recognized, but Python was installed. Which issue is a likely cause?
38
What change fixes the error in this code? total = cost + tax; cost = 20; tax = 3
total after cost and tax
total to Total
cost + tax
+ with =
39
What happens when Python evaluates 3 + '4'?
34
7
3.4
TypeError
40
Which option correctly explains why for = 10 cannot be used as an assignment?
for must always be uppercase
for contains an illegal digit
for is a Python keyword
for is too short
41
The same file contains:
print(3 / 2)
What is printed when the file is executed with standard Python 2.7 and Python 3.x, respectively?
1 in both Python 2.7 and Python 3.x
1 in Python 2.7 and 1.5 in Python 3.x
1.5 in both Python 2.7 and Python 3.x
1.5 in Python 2.7 and 1 in Python 3.x
42
A Windows computer has Python 3.10 and 3.12 registered with the Python Launcher. Which command explicitly runs script.py using Python 3.10 without changing PATH?
py -3.10 script.py
py script.py -3.10
python3.10 -py script.py
python -3.10 script.py
43
The file hello.py contains:
def main():
print("Hello World")
if __name__ == "__main__":
main()
From its directory, it is first run with python hello.py and then in a new process with python -c "import hello". What is printed?
Hello World once by the first command and nothing by the second
Hello World by the second
Hello World once by each of the two commands
Hello World twice by the first command and nothing by the second
44
What does the following Python 3 code print?
first = second = []
first += [1]
first = first + [2]
print(first, second)
[1, 2] [1, 2]
[1, 2] [1]
[1] []
[2] [1]
45
Consider:
available = False
if available:
result = "ready"
print(result)
Which change guarantees that print(result) never raises NameError while still allowing the conditional assignment to replace a default value?
result = "unavailable" before the if statement
if result and available:
print(result) into an else block
global result inside the if statement
46
In Python 3, what is the value of the following expression?
(type(5 / 2).__name__, type(5 // 2).__name__, type(5 + 0j).__name__)
('float', 'int', 'float')
('float', 'float', 'complex')
('int', 'int', 'complex')
('float', 'int', 'complex')
47
What does this code print?
x = [1]
y = x
x = x + [2]
y.append(3)
print(x, y)
[1, 2, 3] [1, 3]
[1, 2, 3] [1, 2, 3]
[1, 3] [1, 2, 3]
[1, 2] [1, 3]
48 Which assignment is syntactically valid in Python 3.12?
True = 4
class = 4
await = 4
match = 4
49 Which line is a syntactically valid complete Python 3 statement?
x = 1; while x: break
x = 1; if True: y = 2
x = 1; for i in range(2): pass
if True: x = 1; y = 2
50
What is the result of the following expression in Python 3?
(-7 // 3, -7 % 3)
(-3, 2)
(-2, -1)
(-3, -1)
(-2, 2)
51
What value is produced by this Python expression?
-2**2**3
64
256
-64
-256
52
What does the following code print?
text = "python"
print(text[-1:1:-2] + text[:0])
no
nho
nh
nhy
53
What are the value and type produced by this composed expression?
str(int(float("3.9")) + len("ab"))
"5" of type str
5.9 of type float
"5.9" of type str
5 of type int
54
What does this valid Python 3 program print?
total = (1 + # first value
2)
label = "a#b" # display label
print(total, label)
2 a#b
3 a#b
1 a#b
3 a
55 On Windows, several Python runtimes are registered with the Python Launcher, but their installation directories are unknown. Which command lists the registered versions together with their executable paths?
where py -0
py --path
python -0p
py -0p
56
What exact character sequence is written before the program terminates?
def emit(value):
print(value, end="")
return value
print(emit(1) + emit(2))
1 2 3 followed by a newline
312 followed by a newline
123 followed by a newline
12 followed by a newline
57
What does the following Python 3 code print?
data = ([1],)
try:
data[0] += [2]
except TypeError:
pass
print(data)
([2],)
([1, 2],)
([1],)
(1, 2)
58
In Python 3, this program raises NameError on its second line:
values = [n * 2 for n in range(3)]
print(n)
Which replacement for the second line prints the final computed value without introducing another variable?
print(values[3])
print(range[-1])
print(values[-1])
print(n[-1])
59
The following source is executed once with Python 2.7 and once with Python 3.x. The user enters 7 each time.
value = input()
print(type(value).__name__)
What type name is printed by each version?
str in both Python 2.7 and Python 3.x
str in Python 2.7 and int in Python 3.x
int in both Python 2.7 and Python 3.x
int in Python 2.7 and str in Python 3.x
60
A beginner saves and runs this Python 3 program:
print = "Hello World"
print(print)
What happens?
SyntaxError because print is a reserved keyword
NameError because print cannot be reassigned
Hello World once and then terminates normally
TypeError because the string is called as a function
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 →