AI-assisted, automatically checked• Updated August 2026

Python MCQs

Comprehensive Python MCQs covering data structures, functions, decorators, generators, and OOP concepts.

55 MCQs2 subtopicsProgrammingAll Levels
Key Concept Overview & Exam Context

Review core principles of Python before answering questions. These questions simulate placement technical assessments (TCS, Infosys, Amazon), university exams (CS/IT), and technical certifications.

Target Readiness
Placements & Job Interviews
Question Format
Multiple-Choice with Explanations
Explained Solutions
Written explanations

Python Essentials Quiz

10 questions · Interactive timed test · Instant score & review

Browse by Subtopic

Python MCQ Questions

Page 1 of 2 (55 total)
Q1

What is the difference between a list and a tuple in Python?

easy
View Answer & Explanation
Correct Answer: A list is mutable and can be changed after creation, while a tuple is immutable
Explanation:
Because tuples cannot be changed after creation, they are often used for fixed collections of values, such as coordinates, and they can also be used as dictionary keys, which lists cannot because lists are unhashable.
Q2

What does the *args syntax allow a Python function to accept?

medium
View Answer & Explanation
Correct Answer: A variable number of positional arguments, collected into a tuple
Explanation:
When a function is defined with *args, any extra positional arguments passed to it are gathered into a tuple inside the function, letting the function accept a flexible number of inputs.
Q3

What is a Python decorator?

hard
View Answer & Explanation
Correct Answer: A function that wraps another function to modify or extend its behavior without changing its actual code
Explanation:
Decorators are applied using the @ symbol above a function definition, and they are commonly used for tasks like logging, timing, or access control without needing to modify the original function's internal logic.
Q4

What is the purpose of the __init__ method in a Python class?

easy
View Answer & Explanation
Correct Answer: It acts as the constructor, initializing a new object's attributes when it is created
Explanation:
The __init__ method is automatically called when a new instance of a class is created, and it is where instance attributes are typically set using the 'self' parameter.
Q5

What is the difference between '==' and 'is' in Python?

medium
View Answer & Explanation
Correct Answer: '==' compares the values of two objects, while 'is' checks whether they are the same object in memory
Explanation:
Two variables can hold equal values but point to two separate objects in memory, in which case '==' returns True but 'is' returns False, since 'is' checks object identity rather than equality of content.
Q6

What is a list comprehension in Python?

medium
View Answer & Explanation
Correct Answer: A concise way to create a list by applying an expression to each item in an iterable, optionally with a condition
Explanation:
List comprehensions provide a shorter, often more readable alternative to writing a full for loop to build a new list, such as [x*2 for x in range(5)] to double each number in a range.
Q7

What does the Python 'self' parameter refer to inside a class method?

easy
View Answer & Explanation
Correct Answer: The current instance of the class on which the method is being called
Explanation:
'self' must be explicitly listed as the first parameter of instance methods in Python, and it is how the method accesses and modifies the specific object's own attributes.
Q8

What is the purpose of Python's 'with' statement when working with files?

medium
View Answer & Explanation
Correct Answer: It ensures the file is automatically closed once the block of code finishes, even if an error occurs
Explanation:
The 'with' statement works with context managers, and for file handling it guarantees proper cleanup, calling the file's close method automatically without requiring an explicit try-finally block.
Q9

What is the output of the following expression in Python: type(5 / 2)?

medium
View Answer & Explanation
Correct Answer: float
Explanation:
In Python 3, the single forward slash always performs true division and returns a float, even when dividing two integers evenly, while the double slash operator performs floor division and returns an integer when both operands are integers.
Q10

What is a Python generator?

hard
View Answer & Explanation
Correct Answer: A function that uses 'yield' to produce a sequence of values lazily, one at a time, instead of returning them all at once
Explanation:
Because a generator produces values on demand rather than building an entire list in memory upfront, it is often more memory efficient when working with large or infinite sequences.
Q11

What is the difference between a shallow copy and a deep copy in Python?

hard
View Answer & Explanation
Correct Answer: A shallow copy duplicates the outer object but still references the same nested objects, while a deep copy duplicates everything recursively
Explanation:
If a list contains other lists, a shallow copy of the outer list still shares the same inner list objects with the original, so changing a nested item affects both, whereas a deep copy creates fully independent copies at every level.
Q12

What does the Python 'pass' statement do?

easy
View Answer & Explanation
Correct Answer: It acts as a placeholder that does nothing, allowing code to run without implementing logic yet
Explanation:
'pass' is often used when a block of code is syntactically required, such as inside an empty function or class body, but the actual logic has not been written yet.
Q13

What is the purpose of Python's try-except block?

easy
View Answer & Explanation
Correct Answer: To catch and handle exceptions that occur during the execution of code, preventing the program from crashing
Explanation:
Code that might raise an error is placed inside the try block, and if an exception occurs, control passes to the matching except block instead of the program terminating abruptly.
Q14

What does the Python built-in function 'zip()' do?

medium
View Answer & Explanation
Correct Answer: Combines multiple iterables element-wise into tuples
Explanation:
zip() pairs up corresponding elements from two or more iterables, such as two lists, producing tuples that can then be looped over together, and it stops once the shortest iterable is exhausted.
Q15

What is the key difference between a Python set and a list?

medium
View Answer & Explanation
Correct Answer: A set stores only unique, unordered elements, while a list can store duplicates in a defined order
Explanation:
Sets automatically discard duplicate values and do not preserve insertion order in the way lists do, which makes them useful for fast membership testing and removing duplicates from a collection.
Q16

What does the Python keyword 'global' do inside a function?

medium
View Answer & Explanation
Correct Answer: It allows the function to modify a variable defined outside its local scope, at the module level
Explanation:
Without the 'global' keyword, assigning a value to a variable inside a function creates a new local variable instead of modifying the module level variable with the same name.
Q17

What is the result of evaluating '3' + '4' in Python?

easy
View Answer & Explanation
Correct Answer: '34'
Explanation:
When applied to strings, the plus operator performs concatenation rather than numeric addition, joining the two string values together into '34' rather than adding them as numbers.
Q18

What is the purpose of the Python 'enumerate()' function?

medium
View Answer & Explanation
Correct Answer: It returns both the index and the value of each item while looping over an iterable
Explanation:
enumerate() is commonly used in a for loop when both the position and the value of each element are needed, avoiding the need to manually track a separate counter variable.
Q19

What does Python's Global Interpreter Lock (GIL) do?

hard
View Answer & Explanation
Correct Answer: It ensures that only one thread executes Python bytecode at a time within a single process
Explanation:
The GIL is a mechanism in the standard CPython implementation that simplifies memory management but also means that CPU-bound multithreaded Python programs do not achieve true parallel execution on multiple cores.
Q20

What is the difference between append() and extend() when used on a Python list?

medium
View Answer & Explanation
Correct Answer: append() adds a single item to the end of the list, while extend() adds all items from an iterable individually
Explanation:
Calling append() with a list as the argument adds that entire list as one single nested element, whereas extend() unpacks the iterable and adds each of its elements separately to the original list.
Q21

What is the output of bool(0) in Python?

easy
View Answer & Explanation
Correct Answer: False
Explanation:
In Python, several values are considered falsy by default, including 0, empty strings, empty lists, and None, meaning bool() converts them to False, while any nonzero number is considered truthy.
Q22

What is a lambda expression in Java used for?

medium
View Answer & Explanation
Correct Answer: It provides a concise way to represent an instance of a functional interface, often for short pieces of behavior
Explanation:
Lambda expressions were introduced to reduce the verbosity of anonymous inner classes, especially when working with interfaces that have a single abstract method, such as Runnable or Comparator.
Q23

What is the purpose of Python's '__str__' method in a class?

medium
View Answer & Explanation
Correct Answer: It defines how an object should be represented as a readable string, mainly for end users
Explanation:
When you call print() or str() on an object, Python looks for a __str__ method to decide what text to display, falling back to a default representation if none is defined.
Q24

What does Python's 'is not' operator check for?

medium
View Answer & Explanation
Correct Answer: Whether two variables do not refer to the same object in memory
Explanation:
Similar to how 'is' checks identity, 'is not' checks the opposite, that two references point to different objects, even if their contents happen to be equal.
Q25

What is the purpose of Python's 'assert' statement?

medium
View Answer & Explanation
Correct Answer: It checks that a condition is true and raises an AssertionError if it is not, often used for debugging
Explanation:
Assertions are commonly used to catch programming errors early, though they are typically disabled in optimized production runs, so they should not replace real error handling.
Q26

What does the Python 'range()' function return?

easy
View Answer & Explanation
Correct Answer: An immutable sequence of numbers that can be iterated over, generated lazily
Explanation:
range() does not create the full list of numbers in memory all at once. Instead it produces values one at a time as they are needed, which makes it memory efficient for large ranges.
Q27

What is the difference between a Python module and a package?

easy
View Answer & Explanation
Correct Answer: A module is a single Python file, while a package is a directory containing multiple modules along with an init file
Explanation:
Packages allow related modules to be organized together under a common namespace, which becomes especially useful as a project grows larger.
Q28

What is the purpose of Python's 'super()' function?

medium
View Answer & Explanation
Correct Answer: It allows a subclass to call methods defined in its parent class, often used inside an overridden method
Explanation:
super() is often used inside __init__ to make sure the parent class is properly initialized before the subclass adds its own setup logic.
Q29

What does Python's 'in' keyword check when used with a list, such as 'x in my_list'?

easy
View Answer & Explanation
Correct Answer: Whether the value x exists as an element somewhere within the list
Explanation:
This membership check scans through the list looking for a matching element and returns True or False, and it works similarly with strings, sets, and dictionaries.
Q30

What is a Python namedtuple used for?

hard
View Answer & Explanation
Correct Answer: It creates a lightweight, immutable object with named fields, combining the simplicity of a tuple with readable attribute access
Explanation:
Instead of accessing elements by numeric index like a regular tuple, a namedtuple lets you access fields by name, which makes code more self-documenting.
Q31

What is the purpose of the Python '__len__' method in a custom class?

hard
View Answer & Explanation
Correct Answer: It defines what the built-in len() function should return when called on an instance of that class
Explanation:
By implementing __len__, a custom class can support len() just like built-in types such as lists and strings do, making the class feel more natural to use.
Q32

What does the Python 'any()' function do when applied to an iterable?

medium
View Answer & Explanation
Correct Answer: It returns True if at least one element in the iterable is truthy
Explanation:
any() short-circuits, meaning it stops checking as soon as it finds one truthy value, which is different from all(), which requires every element to be truthy.
Q33

What is the purpose of Python's 'map()' function?

medium
View Answer & Explanation
Correct Answer: It applies a given function to every item in an iterable and returns an iterator of the results
Explanation:
map() is often used with a lambda or named function to transform each element of a list, such as squaring every number, without writing an explicit loop.
Q34

What is the difference between Python's 'append()' and 'insert()' methods for lists?

easy
View Answer & Explanation
Correct Answer: append() adds an item to the end of the list, while insert() adds an item at a specified index
Explanation:
insert() takes both a position and a value, letting you place a new element anywhere in the list, while append() always places the new element after the last existing one.
Q35

What is the purpose of Python's context manager protocol, implemented with '__enter__' and '__exit__'?

hard
View Answer & Explanation
Correct Answer: It allows an object to manage setup and cleanup automatically when used with the 'with' statement
Explanation:
This is the mechanism that lets file objects automatically close themselves after a 'with open(...) as f:' block finishes, even if an exception occurs inside the block.
Q36

What does the Python expression 'list(range(5))' produce?

easy
View Answer & Explanation
Correct Answer: [0, 1, 2, 3, 4]
Explanation:
range(5) starts counting from zero by default and stops before reaching 5, producing five values in total when converted to a list.
Q37

What is the purpose of Python's 'sorted()' function compared to the list's own 'sort()' method?

medium
View Answer & Explanation
Correct Answer: sorted() returns a new sorted list without changing the original, while sort() sorts a list in place
Explanation:
Because sorted() works on any iterable and returns a fresh list, it can be used on tuples or strings too, while sort() is a method specific to lists and modifies them directly.
Q38

What is a Python f-string used for?

easy
View Answer & Explanation
Correct Answer: It provides a convenient way to embed expressions directly inside string literals for formatting
Explanation:
Introduced in Python 3.6, f-strings let you write something like f'Hello {name}' to insert the value of a variable directly, which is generally more readable than older formatting methods.
Q39

What does the Python 'del' statement do when used on a list element, such as 'del my_list[0]'?

easy
View Answer & Explanation
Correct Answer: It removes the element at that index from the list
Explanation:
del works directly on the object at a given index or key, and after deletion, later elements shift to fill in the gap left behind in the list.
Q40

What is the output of the expression '2 ** 3' in Python?

easy
View Answer & Explanation
Correct Answer: 8
Explanation:
The double asterisk is Python's exponentiation operator, so 2 ** 3 means 2 raised to the power of 3, which equals 8.
Q41

What is the purpose of Python's '*args' and '**kwargs' when combined in a single function definition?

medium
View Answer & Explanation
Correct Answer: They allow a function to accept any number of positional arguments and any number of keyword arguments respectively
Explanation:
*args collects extra positional arguments into a tuple, while **kwargs collects extra keyword arguments into a dictionary, giving functions a great deal of flexibility.
Q42

What is the purpose of Python's 'isinstance()' function?

medium
View Answer & Explanation
Correct Answer: It checks whether an object belongs to a specified class or type, including subclasses
Explanation:
isinstance() is often preferred over comparing type() directly, since it correctly recognizes objects that are instances of a subclass of the specified type as well.
Q43

What does Python's 'dict.get()' method do differently compared to using square brackets to access a dictionary key?

easy
View Answer & Explanation
Correct Answer: get() returns a default value, such as None, instead of raising an error when the key does not exist
Explanation:
Using square brackets like my_dict['key'] raises a KeyError if the key is missing, while my_dict.get('key') quietly returns None or a specified fallback value instead.
Q44

What is the purpose of a Python virtual environment?

medium
View Answer & Explanation
Correct Answer: It creates an isolated space with its own installed packages, separate from the system-wide Python installation
Explanation:
Virtual environments help avoid version conflicts between projects that might need different versions of the same library installed at the same time.
Q45

What does the Python 'continue' statement do inside a loop?

easy
View Answer & Explanation
Correct Answer: It skips the rest of the current iteration and moves on to the next one
Explanation:
Unlike 'break', which exits the loop completely, 'continue' just jumps ahead to the next iteration, skipping any remaining code in the current pass through the loop.
Q46

What is the purpose of Python's 'itertools' module?

hard
View Answer & Explanation
Correct Answer: It provides efficient tools for creating and working with iterators, such as combinations and infinite counters
Explanation:
Functions like itertools.combinations or itertools.chain let you build complex iteration patterns without writing a lot of manual looping logic yourself.
Q47

What is the output of 'type([])' in Python?

easy
View Answer & Explanation
Correct Answer: <class 'list'>
Explanation:
Square brackets with nothing inside create an empty list, so calling type() on it correctly identifies the object as belonging to the list class.
Q48

Which of the following data types in Python is IMMUTABLE?

easy
View Answer & Explanation
Correct Answer: Tuple
Explanation:
Tuples, strings, and integers are immutable in Python, meaning their contents cannot be modified in place after creation. Lists, dicts, and sets are mutable.
Q49

Which of the following built-in data types in Python is immutable?

easy
View Answer & Explanation
Correct Answer: tuple
Explanation:
Tuples, strings, and integers are immutable in Python, whereas lists, dictionaries, and sets are mutable.
Q50

What is the result of list(set([1, 2, 2, 3, 4, 4])) in Python?

easy
View Answer & Explanation
Correct Answer: [1, 2, 3, 4]
Explanation:
A set automatically eliminates duplicate items, resulting in unique elements [1, 2, 3, 4].
Showing 1 to 50 of 55 questions