Python Practice Test: Variables and Data Types
- Variables and Data Types
- 10 questions
- 13 minutes
Ten short questions on how Python stores and converts values. Tip: work out the type of each value before you work out the result.
Tap an option to mark your answer. You can change your mind, or tap the same option again to leave the question blank. The correct answers and explanations appear when you finish the test.Tap an option to answer; tap it again to leave the question blank.
Your result
- 0 correct
- 0 wrong
- 0 blank
-
Question 1
print(type(3 / 2))
What does the code print?
Correct answer: C
The / operator always returns a float in Python 3, even when the result is a whole number.
-
Question 2
x = "5"
y = 3
print(x * y)What does the code print?
Correct answer: A
Multiplying a string by an integer repeats the string, so "5" repeated three times gives 555.
-
Question 3
print(10 // 3, 10 % 3)
What does the code print?
Correct answer: D
// is floor division (3) and % gives the remainder (1).
-
Question 4
Which of these is a valid Python variable name?
Correct answer: B
Names may contain letters, digits and underscores but cannot start with a digit. A hyphen is the minus operator, and class is a reserved keyword.
-
Question 5
print(int("7") + 2.5)
What does the code print?
Correct answer: A
int("7") is 7, and adding a float gives the float 9.5.
-
Question 6
a = [1, 2]
b = a
b.append(3)
print(a)What does the code print?
Correct answer: C
b = a does not copy the list; both names refer to the same object, so appending through b changes what a shows.
-
Question 7
print(bool(""), bool("False"))
What does the code print?
Correct answer: D
An empty string is falsy, but any non-empty string, including the text "False", is truthy.
-
Question 8
print(round(2.5), round(3.5))
What does the code print?
Correct answer: B
Python rounds exact halves to the nearest even number, so 2.5 becomes 2 and 3.5 becomes 4.
-
Question 9
x = 5
x += 2
x *= 3
print(x)What does the code print?
Correct answer: C
x becomes 7 after the first line and 21 after the second.
-
Question 10
print(2 ** 3 ** 2)
What does the code print?
Correct answer: A
The ** operator is evaluated from right to left, so this is 2 ** 9, which equals 512.
Questions you leave blank are not counted as wrong.