Python MCQ Questions and Answers (30 Quiz Questions with Explanations)

Python MCQ Questions with Answers – 30-question quiz with explanations

Quick answer: Below are 30 Python MCQ questions with answers and explanations covering basics, operators, strings, lists, tuples, sets, dictionaries, functions, OOP and exceptions. Output-based questions were run on Python 3.13, so the answers are verified. Try each question first, then check the answer below it.

Contents

Python Basics MCQs

Q1. Who developed the Python programming language?

(a) Dennis Ritchie
(b) Guido van Rossum
(c) James Gosling
(d) Bjarne Stroustrup

Answer: (b) Guido van Rossum – Guido van Rossum created Python and released it in 1991.

Q2. What is the file extension of a Python source file?

(a) .pt
(b) .pyth
(c) .py
(d) .p

Answer: (c) .py – Python scripts are saved with the .py extension.

Q3. How does Python define a block of code?

(a) Curly braces { }
(b) Indentation
(c) Parentheses ( )
(d) The end keyword

Answer: (b) Indentation – Python uses indentation (usually 4 spaces) to mark code blocks.

Q4. Which symbol starts a single-line comment in Python?

(a) //
(b) /*
(c) #
(d) —

Answer: (c) # – Everything after # on a line is a comment.

Q5. Which keyword is used to define a function?

(a) function
(b) def
(c) fun
(d) define

Answer: (b) def – Functions are defined with def name(parameters):

Q6. What does input() return?

(a) int
(b) float
(c) str
(d) depends on what the user types

Answer: (c) str – input() always returns a string; convert it with int() or float() when needed.

Q7. Which of these is NOT a Python keyword?

(a) pass
(b) eval
(c) assert
(d) nonlocal

Answer: (b) eval – eval is a built-in function, not a keyword.

Python Operators and Expressions MCQs

Q8. What is the output?

print(type(10 / 2))

(a) <class ‘int’>
(b) <class ‘float’>
(c) 5
(d) Error

Answer: (b) <class ‘float’> – The / operator always returns a float, so the output is <class ‘float’>.

Q9. What is the output?

print(7 // 2, 7 % 2, 2 ** 3)

(a) 3.5 1 6
(b) 3 1 8
(c) 3 1 6
(d) 4 1 8

Answer: (b) 3 1 8 – // is floor division (3), % is remainder (1), ** is power (8).

Q10. What is the output?

print(2 ** 3 ** 2)

(a) 64
(b) 512
(c) 36
(d) Error

Answer: (b) 512 – ** is right-associative: 3 ** 2 = 9, then 2 ** 9 = 512.

Q11. What is the output?

print(0.1 + 0.2 == 0.3)

(a) True
(b) False
(c) Error
(d) None

Answer: (b) False – Floating-point rounding makes 0.1 + 0.2 equal 0.30000000000000004, so the result is False.

Q12. What does the is operator compare?

(a) Values
(b) Types
(c) Object identity (same object in memory)
(d) Lengths

Answer: (c) Object identity (same object in memory) – is checks whether two names refer to the same object; == compares values.

Python Strings and Lists MCQs

Q13. What is the output?

print("Py" * 3)

(a) Py3
(b) PyPyPy
(c) Py Py Py
(d) Error

Answer: (b) PyPyPy – Multiplying a string by an integer repeats it.

Q14. What is the output?

print(3 * "a" + "b")

(a) ab ab ab
(b) aaab
(c) 3ab
(d) Error

Answer: (b) aaab – “a” is repeated three times and then “b” is added: aaab.

Q15. What is the output?

print("python"[1:4])

(a) pyt
(b) yth
(c) ytho
(d) pyth

Answer: (b) yth – Slicing [1:4] takes indices 1, 2 and 3: y, t, h.

Q16. What is the output of len(“Engineers”)?

(a) 8
(b) 9
(c) 10
(d) Error

Answer: (b) 9 – “Engineers” has 9 characters.

Q17. What is the output?

x = [1, 2, 3]
print(x[-1])

(a) 1
(b) 3
(c) -1
(d) Error

Answer: (b) 3 – Negative indexing starts from the end; x[-1] is the last element, 3.

Q18. Which method adds an element to the end of a list?

(a) add()
(b) insert()
(c) append()
(d) push()

Answer: (c) append() – list.append(item) adds one item at the end.

Q19. What is the output?

print(list(range(2, 10, 3)))

(a) [2, 5, 8]
(b) [2, 5, 8, 11]
(c) [3, 6, 9]
(d) [2, 3, 4]

Answer: (a) [2, 5, 8] – range(start, stop, step) starts at 2 and adds 3 each time while staying below 10.

Python Tuples, Sets and Dictionaries MCQs

Q20. Which of these data types is immutable?

(a) list
(b) dict
(c) set
(d) tuple

Answer: (d) tuple – Tuples cannot be changed after creation.

Q21. What is the output?

a = (1,)
print(type(a))

(a) <class ‘int’>
(b) <class ‘tuple’>
(c) <class ‘list’>
(d) Error

Answer: (b) <class ‘tuple’> – The trailing comma makes (1,) a single-element tuple.

Q22. What is the output?

print(len({1, 1, 2, 3}))

(a) 4
(b) 3
(c) 2
(d) Error

Answer: (b) 3 – Sets keep only unique values: {1, 2, 3}.

Q23. Which collection does NOT allow duplicate values?

(a) list
(b) tuple
(c) set
(d) string

Answer: (c) set – A set stores only unique elements.

Q24. What is the output?

d = {"a": 1}
print(d.get("b", 0))

(a) None
(b) KeyError
(c) 0
(d) b

Answer: (c) 0 – get() returns the default value (0) when the key is missing.

Q25. What is the output?

print(bool([]))

(a) True
(b) False
(c) None
(d) Error

Answer: (b) False – Empty collections are False in a boolean context.

Python Functions, OOP and Exceptions MCQs

Q26. Which exception is raised by 10 / 0?

(a) ValueError
(b) ZeroDivisionError
(c) ArithmeticOverflow
(d) TypeError

Answer: (b) ZeroDivisionError – Dividing by zero raises ZeroDivisionError.

Q27. Which block always runs, whether or not an exception occurs?

(a) except
(b) else
(c) finally
(d) raise

Answer: (c) finally – finally runs in all cases and is used for clean-up.

Q28. What is __init__ in a Python class?

(a) A destructor
(b) A constructor/initialiser called when an object is created
(c) A static method
(d) A module

Answer: (b) A constructor/initialiser called when an object is created – __init__ initialises a new object’s attributes.

Q29. Which keyword turns a function into a generator?

(a) return
(b) yield
(c) lambda
(d) async

Answer: (b) yield – A function containing yield returns a generator.

Q30. Which library is mainly used for numerical arrays in Python?

(a) Pandas
(b) NumPy
(c) Matplotlib
(d) Flask

Answer: (b) NumPy – NumPy provides fast n-dimensional arrays and numerical functions.

Score Yourself

ScoreLevelNext step
25–30Strong basicsMove to interview questions and projects
15–24Good, with gapsRevise data types and practise programs
Below 15BeginnerFollow the Python roadmap from week 1

Related Python Guides

Learn Python With Guidance

Join our Python course or message us on WhatsApp for doubt-clearing.

Scroll to Top