0% found this document useful (0 votes)
9 views4 pages

Python Mock Interview Questions & Answers

The document is a Python Mock Interview Guide containing over 50 questions and detailed answers categorized by experience level. It covers key Python features, data types, memory management, exception handling, and coding challenges with solutions. The guide aims to prepare candidates for Python interviews by providing essential concepts and practical coding examples.

Uploaded by

rajeshrd1116
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Python Mock Interview Questions & Answers

The document is a Python Mock Interview Guide containing over 50 questions and detailed answers categorized by experience level. It covers key Python features, data types, memory management, exception handling, and coding challenges with solutions. The guide aims to prepare candidates for Python interviews by providing essential concepts and practical coding examples.

Uploaded by

rajeshrd1116
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Mock Interview Guide (50+ Questions with

Detailed Answers)

Basics (0–1 Year Experience)

Q: What are Python’s key features?


A: Python is an interpreted, dynamically typed language with easy-to-read syntax. It supports multiple
paradigms (OOP, functional, procedural), has an extensive standard library, and is cross-platform.

Q: Explain Python data types.


A: Python has several built-in data types: Numeric (int, float, complex), Sequence (list, tuple, range),
Mapping (dict), Set types (set, frozenset), Boolean (bool), and Text (str).

Q: Difference between list, tuple, and set.


A: List: Ordered, mutable, allows duplicates. Tuple: Ordered, immutable, allows duplicates. Set:
Unordered, mutable, no duplicates.

Q: Write a program to reverse a string.


A: Example: def reverse_string(s): return s[::-1] print(reverse_string('hello'))

Q: Explain Python’s memory management.


A: Python uses reference counting and garbage collection. Objects are stored in private heaps
managed by the Python memory manager.

Q: What is PEP 8 and why is it important?


A: PEP 8 is Python's style guide. It ensures code readability and consistency across projects.

Q: Explain Python’s indentation rules.


A: Indentation is mandatory in Python and defines code blocks. Typically, 4 spaces are used.

Q: What are Python’s built-in data structures?


A: Lists, tuples, sets, and dictionaries are the primary built-in data structures.

Q: Difference between mutable and immutable objects.


A: Mutable objects can change their state (e.g., lists), while immutable objects cannot (e.g., strings,
tuples).

Q: Explain Python’s dynamic typing.


A: Variables in Python do not require explicit type declaration; types are determined at runtime.

Intermediate (1–3 Years Experience)

Q: Explain Python’s GIL.


A: The Global Interpreter Lock ensures only one thread executes Python bytecode at a time, limiting
true parallelism in CPU-bound tasks.

Q: What are Python decorators?


A: Decorators are functions that modify other functions without changing their code. Example: def
decorator(func): def wrapper(): print('Before function') func() print('After function') return wrapper
@decorator def say_hello(): print('Hello!')

Q: Difference between is and ==.


A: 'is' checks object identity (same memory location), while '==' checks value equality.

Q: Explain Python’s exception handling.


A: Python uses try-except blocks for exception handling. Example: try: x = 1/0 except
ZeroDivisionError: print('Cannot divide by zero')

Q: What is a generator in Python?


A: Generators are iterators that yield items one at a time using 'yield', saving memory.

Q: Explain list comprehension with an example.


A: List comprehension provides a concise way to create lists. Example: squares = [x**2 for x in
range(5)]

Q: Difference between deep copy and shallow copy.


A: Shallow copy copies the object but references nested objects; deep copy copies everything
recursively.

Q: Explain Python’s garbage collection.


A: Python uses reference counting and a cyclic garbage collector to manage memory.

Q: What is the difference between Python 2 and Python 3?


A: Python 3 introduced print() as a function, better Unicode support, and removed old constructs like
xrange.

Q: Explain Python’s context managers.


A: Context managers handle resource setup and cleanup using 'with' statements. Example: with
open('[Link]') as f: data = [Link]()

Senior-Level (3–4 Years Experience)

Q: Explain Python’s memory management in detail.


A: Python uses private heaps, reference counting, and garbage collection for memory management.

Q: What is asyncio and when to use it?


A: Asyncio enables asynchronous programming using coroutines, ideal for I/O-bound tasks.

Q: Explain Python’s multithreading vs multiprocessing.


A: Multithreading is limited by GIL for CPU-bound tasks; multiprocessing creates separate processes
for true parallelism.

Q: How do you optimize Python code for performance?


A: Use built-in functions, avoid unnecessary loops, leverage NumPy, use generators, and profile code.

Q: Explain Python’s metaclasses.


A: Metaclasses define the behavior of classes themselves, allowing customization of class creation.

Q: What is monkey patching in Python?


A: Monkey patching means dynamically modifying a class or module at runtime.

Q: Explain Python’s descriptor protocol.


A: Descriptors manage attribute access using __get__, __set__, and __delete__ methods.

Q: How do you handle large datasets in Python?


A: Use pandas, chunk processing, memory-efficient structures, and multiprocessing.

Q: Explain Python’s type hints and annotations.


A: Type hints improve code readability and enable static type checking.

Q: What is the difference between @staticmethod, @classmethod,


and instance methods?
A: @staticmethod does not access class or instance, @classmethod accesses class, instance methods
access instance data.

Coding Challenges with Solutions


Challenge: Reverse a string without slicing.
Solution: def reverse_string(s): result = '' for char in s: result = char + result return result

Challenge: Find duplicates in a list.


Solution: def find_duplicates(lst): seen = set() duplicates = set() for item in lst: if item in seen:
[Link](item) else: [Link](item) return list(duplicates)

Challenge: Implement a simple cache using a decorator.


Solution: def cache(func): memo = {} def wrapper(*args): if args in memo: return memo[args] result =
func(*args) memo[args] = result return result return wrapper

Challenge: Check if a string is a palindrome.


Solution: def is_palindrome(s): s = [Link]() return s == s[::-1]

Challenge: Write a program to merge two dictionaries.


Solution: dict1 = {'a': 1, 'b': 2} dict2 = {'b': 3, 'c': 4} merged = {**dict1, **dict2}

You might also like