Python MCQ Test
20 random Python MCQs with answers: syntax, data types, strings, lists, tuples, sets, dictionaries, loops, functions, files, exceptions and classes.
- 20 random questions
- 20 minutes
- 60 in the question bank
No sign-up needed. Answers and explanations are shown at the end.
About this test
Python questions in exams love the small details: mutable vs immutable types, slicing, dictionary methods and what a function returns when you forget return. This test covers all of them, plus files, exceptions and classes.
You can try any snippet in the free Python compiler on this site.
Each attempt picks new questions at random from the question bank. You can go back and change answers until you finish or the timer runs out. At the end you see your score, the right answer and a short explanation for every question, and the lessons to revise for the ones you missed.
Topics covered
- Python Introduction
- Installing Python
- Python Syntax
- Python Variables
- Python Data Types
- Python Strings
- Python Numbers
- Python Lists
- Python Tuples and Sets
- Python Dictionaries
- Python If/Else
- Python Loops
- Python Functions
- Python Modules
- Python Files
- Python Errors and Exceptions
- Python Classes and Objects
- Python pip and Packages
- Python JSON and APIs
- Python Project: Simple Expense Tracker
Sample questions with answers
1. What does indentation do in Python?
- Nothing, it's style only
- It defines code blocks
- It marks comments
Show answer
B. It defines code blocks — Inconsistent indentation causes errors.
2. On Windows, which installer option is important?
- Disable pip
- Add python.exe to PATH
- Install for Python 2
Show answer
B. Add python.exe to PATH — Otherwise the terminal can't find python.
3. Python is case-sensitive, so Name and name are…
- Errors
- The same variable
- Different variables
Show answer
C. Different variables — Use consistent lowercase_with_underscores.
4. How do you swap a and b?
a, b = b, aswap(a, b)a = b; b = a
Show answer
A. a, b = b, a — Tuple unpacking swaps in one line.
5. What is type(3.0)?
- decimal
- int
- float
Show answer
C. float — Numbers with a decimal point are floats.
6. What does "python"[1:4] return?
- "ytho"
- "yth"
- "pyt"
Show answer
B. "yth" — The end index is excluded.
7. What does 7 // 2 give?
- 3
- 3.5
- 4
Show answer
A. 3 — // is floor division; / always gives a float.
8. How do you add an item to the end of a list?
items.push(x)items.append(x)items.add(x)
Show answer
B. items.append(x) — insert(i, x) adds at a position.
9. What's special about a set?
- It keeps insertion order for indexing
- It can't change
- No duplicates and fast membership checks
Show answer
C. No duplicates and fast membership checks — {1, 2, 2} becomes {1, 2}.
10. How do you read a key safely with a default?
d.read("key")d.get("key", 0)d["key"] or 0
Show answer
B. d.get("key", 0) — d["missing"] raises KeyError.