Python basics MCQ questions with answers
Variables, data types, type conversion and print: pick an answer to see why it is right.
0 of 20 answered
Question 1 · Easy
What is the output of this code?
x = 5 y = "5" print(type(x), type(y))
Show the answer
Answer: B
Run this code in the editortype()returns a class, which prints as<class 'int'>. The quotes makeya string, so its type isstr. The<type ...>form is how Python 2 printed types.Question 2 · Easy
What is the output of this code?
print("A", "B", "C", sep="-", end="|") print("D")
Show the answer
Answer: A
Run this code in the editorsepgoes between the values andendreplaces the usual newline, so the first call printsA-B-C|without a line break. The secondprintcontinues on the same line.Question 3 · Easy
What is the output of this code?
print(int(7.9), int(-7.9))
Show the answer
Answer: C
Run this code in the editorint()drops the fractional part, which truncates toward zero: 7.9 becomes 7 and -7.9 becomes -7. It does not round, and it does not floor -7.9 down to -8.Question 4 · Easy
What is the output of this code?
a = "3" b = "4" print(a + b) print(int(a) + int(b))
Show the answer
Answer: D
Run this code in the editor+on two strings joins them, giving34. Afterint()converts both values,+adds numbers and gives 7.Question 5 · Easy
What is the output of this code?
price = 49.5 qty = 3 print(f"Total: {price * qty:.2f}")
Show the answer
Answer: B
The f-string evaluates
Run this code in the editorprice * qty(148.5), and the format spec.2fshows exactly two decimal places, so it prints148.50. Without thefprefix the braces would print as typed.Question 6 · Medium
What is the output of this code?
print(bool("False"), bool(0.0), bool(" "))
Show the answer
Answer: C
Any non-empty string is truthy, even
Run this code in the editor"False"and a single space; only the empty string""is falsy.0.0is zero, sobool(0.0)isFalse.Question 7 · Medium
What is the output of this code?
a, b = 10, 20 a, b = b, a + b print(a, b)
Show the answer
Answer: D
The right-hand side is evaluated in full before anything is assigned, so
Run this code in the editora + buses the old values (10 + 20). Doing the two assignments one after the other would give20 40.Question 8 · Medium
What is the output of this code?
items = ["pen", "ink", "nib"] print(*items, sep=", ") print(items)
Show the answer
Answer: A
Run this code in the editor*itemsunpacks the list into three separate arguments, sosepis placed between them. The secondprintreceives the list itself and shows it with brackets and quotes.Question 9 · Hard
What is the output of this code?
name = "Asha" score = 7 print(f"{name=}, {score * 2 = }")
Show the answer
Answer: B
A trailing
Run this code in the editor=in an f-string field prints the expression text and then its value usingrepr(), so the string keeps its quotes. Spaces around the=are kept exactly as written, which is why the second field showsscore * 2 = 14.Question 10 · Hard
What is the output of this code?
print(round(2.5), round(3.5), round(-0.5))
Show the answer
Answer: C
Run this code in the editorround()with no digits argument sends an exact half to the nearest even integer (banker's rounding): 2.5 becomes 2, 3.5 becomes 4 and -0.5 becomes 0. Halves are not always rounded up.Question 11 · Medium
What is the output of this code?
a = b = [1, 2] b.append(3) print(a)
Show the answer
Answer: A
Chained assignment binds both names to the same list object; it does not make a copy. Appending through
Run this code in the editorbchanges that one list, soashows the 3 as well.Question 12 · Hard
What is the output of this code?
print(True + True * 3) print(int("101", 2) + int(True))
Show the answer
Answer: D
Run this code in the editorboolis a subclass ofint, soTruecounts as 1, and*runs before+: 1 + 3 = 4.int("101", 2)reads the text in base 2, which is 5, andint(True)is 1, giving 6.Question 13 · Easy
Which error does this code raise?
age = 16 print("Age: " + age)
Show the answer
Answer: C, TypeError: can only concatenate str (not "int") to str
Run this code in the editor+cannot join astrand anint, so Python raisesTypeError. Write"Age: " + str(age), use an f-string, or pass both values toprintseparated by a comma.Question 14 · Medium
Which error does this code raise?
value = int("3.5") print(value)
Show the answer
Answer: B, ValueError: invalid literal for int() with base 10: '3.5'
Run this code in the editorint()accepts a string only if it looks like a whole number."3.5"has a decimal point, so it raisesValueErrorinstead of truncating.int(float("3.5"))gives 3.Question 15 · Medium
Which error does this code raise?
2nd_place = "Ravi" print(2nd_place)
Show the answer
Answer: A, SyntaxError: invalid decimal literal
A variable name cannot start with a digit, so the program is rejected before any line runs. Python reports a
Run this code in the editorSyntaxError, not aNameError, because2nd_placeis not a valid name at all.Question 16 · Easy
A program runs
age = input("Age: ")and the user types25. What is the type ofage?Show the answer
Answer: D, str
input()always returns a string, whatever the user types. Convert it withint(age)before doing arithmetic with it.Question 17 · Easy
Which of these is a valid Python variable name?
Show the answer
Answer: B, _score2
Names may contain letters, digits and underscores but cannot start with a digit, so
_score2is valid.my-scorereads as a subtraction, andclassis a reserved keyword.Question 18 · Medium
Which of these built-in types is immutable, so its items cannot be changed after it is created?
Show the answer
Answer: C, tuple
A tuple cannot be changed in place: assigning to
t[0]raisesTypeError. Lists, dicts and sets all have methods that change them, such asappend,updateandadd.Question 19 · Medium
Which line prints exactly
5 + 3 = 8?Show the answer
Answer: D
printputs a space between its arguments, and5 + 3is worked out before printing, giving5 + 3 = 8. Joiningstr(5) + str(3)makes53, and adding anintto astrraisesTypeError.Question 20 · Hard
Given
pi = 3.14159, which line prints3.14?Show the answer
Answer: A
.2fmeans two digits after the decimal point..2without thefmeans two significant digits, giving3.1, and2fsets a minimum width of 2 with the default six decimals, giving3.141590.
Keep going
- Read the lesson behind these questions: Python basics in the PythonBook
- Next topic: Python operators MCQ questions with answers
- Write the code yourself in the Python coding challenges, which check your solution with tests.