Unit 1: Setting up your Programming Environment; Variables, Expression and Statements - Practice Quiz

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

1 Which major Python version is recommended for writing modern Python programs?

Python versions Easy
A. Python 4
B. Python 1
C. Python 2
D. Python 3

2 Which command commonly checks the installed Python version in Windows Command Prompt?

Python on Windows Easy
A. python --check
B. python --start
C. python --version
D. python --install

3 Which Python statement correctly displays Hello, World!?

Running a 'Hello World' program Easy
A. write("Hello, World!")
B. print("Hello, World!")
C. show("Hello, World!")
D. display("Hello, World!")

4 Which statement correctly assigns the value 25 to a variable named age?

Naming and using variables Easy
A. age == 25
B. 25 = age
C. age = 25
D. age :=: 25

5 What should you do before using a variable in a Python expression?

Avoiding NameError when using variables Easy
A. Follow it with parentheses
B. Assign it a value
C. Place it in quotes
D. Write it in capitals

6 What is the Python type of the value 42?

Values and types Easy
A. bool
B. int
C. float
D. str

7 What is the Python type of the value 3.14?

Values and types Easy
A. bool
B. str
C. float
D. int

8 What is a variable in Python?

Variables Easy
A. A mark that begins a comment
B. A symbol that ends a program
C. A name that refers to a value
D. A command that installs Python

9 Which of the following is a valid Python variable name?

Variable names and keywords Easy
A. student_name
B. class
C. 2students
D. student-name

10 Why should Python keywords such as if and class not be used as variable names?

Variable names and keywords Easy
A. They cannot appear in programs
B. They contain lowercase letters
C. They have reserved meanings
D. They are too short to use

11 Which line is an assignment statement in Python?

Statements Easy
A. "score"
B. score + 10
C. # score
D. score = 10

12 In the expression 8 + 2, which symbol is the operator?

Operators and operands Easy
A. 2
B. 10
C. 8
D. +

13 Which Python operator performs multiplication?

Operators and operands Easy
A. -
B. /
C. +
D. *

14 What is the result of 2 + 3 * 4 in Python?

Order of operations Easy
A. 11
B. 14
C. 20
D. 24

15 What is the result of (2 + 3) * 4 in Python?

Order of operations Easy
A. 14
B. 18
C. 24
D. 20

16 What is the result of "Py" + "thon"?

Operations on strings Easy
A. "Py+thon"
B. "Python"
C. "thonPy"
D. "Py thon"

17 What is the result of "Hi" * 3?

Operations on strings Easy
A. "HHHiii"
B. "HiHiHi"
C. "Hi3"
D. "Hi Hi Hi"

18 Which statement uses an expression as an argument to print()?

Composition Easy
A. 2 + 3 = print
B. print = 2 + 3
C. print: 2 + 3
D. print(2 + 3)

19 Which symbol begins a single-line comment in Python?

Comments Easy
A. //
B. **
C. <!--
D. #

20 What is the main purpose of comments in a Python program?

Comments Easy
A. To perform calculations
B. To create new variables
C. To display program output
D. To explain code to readers

21 In Python 3, what is the result of the expression 5 / 2?

Python versions Medium
A. 2.0
B. 2.5
C. 3
D. 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?

Python on Windows Medium
A. python3.start calculate.py
B. py -3 calculate.py
C. execute -py calculate.py
D. 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?

Running a 'Hello World' program Medium
A. python hello.py
B. open hello.py
C. start-python hello.py
D. runfile hello.py

24 What is printed by this code? price = 8; quantity = 3; total = price * quantity; print(total)

Naming and using variables Medium
A. 24
B. 11
C. 8 * 3
D. 83

25 Which revision avoids a NameError in the following code? print(message) followed by message = 'Ready'

Avoiding NameError when using variables Medium
A. Change message to a comment
B. Move print(message) below the assignment
C. Replace message with Message
D. Add a space in message

26 What type does type(7.0) report in Python?

Values and types Medium
A. decimal
B. number
C. float
D. int

27 What is printed by this code? score = 10; score = score + 5; print(score)

Variables Medium
A. 15
B. 105
C. score + 5
D. 10

28 Which statement correctly assigns the value 25 to a valid Python variable?

Variable names and keywords Medium
A. class = 25
B. 2nd_place = 25
C. total-score = 25
D. total_score = 25

29 How many Python statements are executed by the following code? x = 4; y = 6; print(x + y)

Statements Medium
A. Two
B. Four
C. Three
D. One

30 In the expression result = 12 - 5, which items are the operands of the subtraction operator?

Operators and operands Medium
A. - and =
B. result and =
C. 12 and 5
D. result and 12

31 What is the value of 3 + 4 * 2 ** 2?

Order of operations Medium
A. 19
B. 28
C. 49
D. 64

32 What is printed by this code? word = 'Py'; print(word * 3)

Operations on strings Medium
A. Py Py Py
B. Py*Py*Py
C. PyPyPy
D. Py3

33 What is the result of 'data' + 'base' in Python?

Operations on strings Medium
A. An error because strings cannot use +
B. data+base
C. data base
D. database

34 What is printed by this code? name = 'Mina'; greeting = 'Hi, ' + name + '!'; print(greeting)

Composition Medium
A. Hi, name!
B. Hi, + Mina + !
C. Hi, Mina!
D. 'Hi, Mina!'

35 What is printed by the following code? x = 4 # initial value followed by print(x + 1)

Comments Medium
A. 4
B. Nothing
C. 5
D. initial value

36 Which statement best describes why Python version selection matters when running a program?

Python versions Medium
A. Python programs run without an interpreter
B. Only the computer's screen resolution changes
C. Some syntax and library behavior differ between versions
D. Every Python version uses identical syntax

37 A Windows terminal reports that python is not recognized, but Python was installed. Which issue is a likely cause?

Python on Windows Medium
A. The file has too many comments
B. Python was not added to the system PATH
C. The monitor is not connected
D. The keyboard is using uppercase letters

38 What change fixes the error in this code? total = cost + tax; cost = 20; tax = 3

Avoiding NameError when using variables Medium
A. Put the assignment to total after cost and tax
B. Rename total to Total
C. Add quotation marks around cost + tax
D. Replace + with =

39 What happens when Python evaluates 3 + '4'?

Values and types Medium
A. It produces the string 34
B. It produces the integer 7
C. It produces the float 3.4
D. It raises a TypeError

40 Which option correctly explains why for = 10 cannot be used as an assignment?

Variable names and keywords Medium
A. for must always be uppercase
B. for contains an illegal digit
C. for is a Python keyword
D. for is too short

41 The same file contains:

PYTHON
print(3 / 2)



What is printed when the file is executed with standard Python 2.7 and Python 3.x, respectively?

Python versions Hard
A. 1 in both Python 2.7 and Python 3.x
B. 1 in Python 2.7 and 1.5 in Python 3.x
C. 1.5 in both Python 2.7 and Python 3.x
D. 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?

Python on Windows Hard
A. py -3.10 script.py
B. py script.py -3.10
C. python3.10 -py script.py
D. python -3.10 script.py

43 The file hello.py contains:

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

Running a 'Hello World' program Hard
A. Hello World once by the first command and nothing by the second
B. Nothing by the first command and Hello World by the second
C. Hello World once by each of the two commands
D. Hello World twice by the first command and nothing by the second

44 What does the following Python 3 code print?

PYTHON
first = second = []
first += [1]
first = first + [2]
print(first, second)

Naming and using variables Hard
A. [1, 2] [1, 2]
B. [1, 2] [1]
C. [1] []
D. [2] [1]

45 Consider:

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

Avoiding NameError when using variables Hard
A. Insert result = "unavailable" before the if statement
B. Replace the condition with if result and available:
C. Move print(result) into an else block
D. Insert global result inside the if statement

46 In Python 3, what is the value of the following expression?

PYTHON
(type(5 / 2).__name__, type(5 // 2).__name__, type(5 + 0j).__name__)

Values and types Hard
A. ('float', 'int', 'float')
B. ('float', 'float', 'complex')
C. ('int', 'int', 'complex')
D. ('float', 'int', 'complex')

47 What does this code print?

PYTHON
x = [1]
y = x
x = x + [2]
y.append(3)
print(x, y)

Variables Hard
A. [1, 2, 3] [1, 3]
B. [1, 2, 3] [1, 2, 3]
C. [1, 3] [1, 2, 3]
D. [1, 2] [1, 3]

48 Which assignment is syntactically valid in Python 3.12?

Variable names and keywords Hard
A. True = 4
B. class = 4
C. await = 4
D. match = 4

49 Which line is a syntactically valid complete Python 3 statement?

Statements Hard
A. x = 1; while x: break
B. x = 1; if True: y = 2
C. x = 1; for i in range(2): pass
D. if True: x = 1; y = 2

50 What is the result of the following expression in Python 3?

PYTHON
(-7 // 3, -7 % 3)

Operators and operands Hard
A. (-3, 2)
B. (-2, -1)
C. (-3, -1)
D. (-2, 2)

51 What value is produced by this Python expression?

PYTHON
-2**2**3

Order of operations Hard
A. 64
B. 256
C. -64
D. -256

52 What does the following code print?

PYTHON
text = "python"
print(text[-1:1:-2] + text[:0])

Operations on strings Hard
A. no
B. nho
C. nh
D. nhy

53 What are the value and type produced by this composed expression?

PYTHON
str(int(float("3.9")) + len("ab"))

Composition Hard
A. Value "5" of type str
B. Value 5.9 of type float
C. Value "5.9" of type str
D. Value 5 of type int

54 What does this valid Python 3 program print?

PYTHON
total = (1 +  # first value
         2)
label = "a#b"  # display label
print(total, label)

Comments Hard
A. 2 a#b
B. 3 a#b
C. 1 a#b
D. 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?

Python on Windows Hard
A. where py -0
B. py --path
C. python -0p
D. py -0p

56 What exact character sequence is written before the program terminates?

PYTHON
def emit(value):
    print(value, end="")
    return value

print(emit(1) + emit(2))

Composition Hard
A. 1 2 3 followed by a newline
B. 312 followed by a newline
C. 123 followed by a newline
D. 12 followed by a newline

57 What does the following Python 3 code print?

PYTHON
data = ([1],)
try:
    data[0] += [2]
except TypeError:
    pass
print(data)

Variables Hard
A. ([2],)
B. ([1, 2],)
C. ([1],)
D. (1, 2)

58 In Python 3, this program raises NameError on its second line:

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

Avoiding NameError when using variables Hard
A. print(values[3])
B. print(range[-1])
C. print(values[-1])
D. 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.

PYTHON
value = input()
print(type(value).__name__)



What type name is printed by each version?

Python versions Hard
A. str in both Python 2.7 and Python 3.x
B. str in Python 2.7 and int in Python 3.x
C. int in both Python 2.7 and Python 3.x
D. int in Python 2.7 and str in Python 3.x

60 A beginner saves and runs this Python 3 program:

PYTHON
print = "Hello World"
print(print)



What happens?

Running a 'Hello World' program Hard
A. It raises SyntaxError because print is a reserved keyword
B. It raises NameError because print cannot be reassigned
C. It prints Hello World once and then terminates normally
D. It raises TypeError because the string is called as a function