Python MCQ questions with answers
Pick a topic, choose an answer and see why it is right, with code you can run in the editor.
Quick test
Section quizzes
Basics
- Python basics
x = 5 y = "5" print(type(x), type(y))
What is the output of this code? - Operators
print(17 // 5, 17 % 5)
What is the output of this code? - Strings
s = "PYTHON" print(s[0], s[-1], s[2])
What is the output of this code? - Conditionals
x = 10 if x > 5 print("big")
Which error does this code raise? - Loops
for i in range(1, 10, 3): print(i, end=" ")
What is the output of this code?
Lists, tuples, dictionaries and sets
- Lists
nums = [10, 20, 30, 40, 50] print(nums[1:4], nums[-1])
What is the output of this code? - Tuples
t = (1, 2, (3, 4), [5, 6]) print(len(t))
What is the output of this code? - Dictionaries
marks = {'Ravi': 70, 'Meena': 85} marks.update({'Ravi': 75, 'John': 60}) print(marks)
What is the output of this code? - Sets
nums = [3, 1, 3, 2, 1, 3] unique = set(nums) print(len(unique), sorted(unique))
What is the output of this code? - Comprehensions
squares = [n ** 2 for n in range(5)] print(squares)
What is the output of this code?
Functions, errors, classes and files
- Functions
def show(x): print(x * 2) print(show(4))
What is the output of this code? - Lambda, map and filter
square = lambda x: x * x print(square(4) + square(2))
What is the output of this code? - Exceptions
marks = [90, 85, 77] print(marks[3]) print(int("A"))
Which error does this code raise? - Classes and objects
class Greeter: def hello(): return "hello" g = Greeter() print(g.hello())
Which error does this code raise? - File handling
with open("items.txt", "w") as f: f.writelines(["pen", "ink", "nib"]) with open("items.txt") as f: print(f.readlines())
What is the output of this code?
Questions
Are the answers checked?
Yes. Every question with code is run in Python 3 before it is published, and the marked answer must be exactly what the code prints or the error it raises.
Do these questions cover the CBSE Class 11 and 12 syllabus?
They cover the Python topics the CBSE syllabus and most first-year programming courses teach: data types, operators, strings, conditions, loops, lists, tuples, dictionaries, functions, exceptions and file handling. Each topic page links the lesson that teaches it.
Can I see why an answer is right?
Every question has an explanation of the right answer and the tempting wrong one, and the code opens in the editor so you can change it and run it again.
For a question a day, try the daily Python puzzle; to write code that is checked by tests, the coding challenges.