SECTION 4 – Advanced Python (Q61–Q80)
Real-world interview style • Big MNC / Big-4 level • Beginner → Advanced
Format: Q → Explanation (5+ lines) → Example → Tip
Q61. What is the Global Interpreter Lock (GIL) and why does it matter?
Explanation:
The Global Interpreter Lock (GIL) is a mutex in CPython that allows only one thread to execute Python bytecode at a time.
It exists to simplify memory management and make Python thread-safe.
Because of the GIL, CPU-bound multithreaded programs do not scale across cores.
However, I/O-bound programs still benefit from threading.
For CPU-heavy tasks, multiprocessing is preferred.
Example:
import threading def task(): for i in range(1000000): pass t1 = threading.Thread(target=task) t2 = threading.Thread(target=task) t1.start() t2.start()
Tip:
In interviews say: Threading for I/O, multiprocessing for CPU.
Q62. What is a lambda function and where is it used?
Explanation:
A lambda function is an anonymous one-line function.
It is commonly used with map(), filter(), and sorted().
Lambda improves readability for small operations.
It cannot contain multiple statements.
Frequently used in data science pipelines.
Example:
square = lambda x: x*x print(square(5))
Tip:
Lambda = short temporary function.
Q63. Explain map(), filter(), and reduce().
Explanation:
map() applies a function to each element.
filter() selects elements based on condition.
reduce() aggregates values into one result.
They support functional programming.
Used heavily in data transformations.
Example:
from functools import reduce nums = [1,2,3,4] print(list(map(lambda x:x*2, nums))) print(list(filter(lambda x:x>2, nums))) print(reduce(lambda a,b:a+b, nums))
Tip:
Mention functional programming concept.
Q64. What is pickle in Python?
Explanation:
Pickle serializes Python objects to binary format.
Used to save ML models.
Not secure for untrusted sources.
Allows object persistence.
Common in deployment pipelines.
Example:
import pickle data = {"a":1} pickle.dump(data, open("file.pkl","wb"))
Tip:
Never load pickle from unknown sources.
Q65. What is virtual environment?
Explanation:
Virtual environments isolate project dependencies.
Prevent version conflicts.
Each project gets its own packages.
Essential in production systems.
Created using venv or virtualenv.
Example:
python -m venv myenv
Tip:
Always use venv in real projects.
Q66. What is name == "main"?
Explanation:
This checks whether file is run directly.
Prevents code from executing during imports.
Improves modularity.
Used in scripts and libraries.
Common interview question.
Example:
if __name__ == "__main__": print("Running directly")
Tip:
Explain module execution.
Q67. What is monkey patching?
Explanation:
Monkey patching modifies classes or functions at runtime.
Used in testing.
Dangerous in production.
Can cause unexpected behavior.
Advanced Python concept.
Example:
class A: def show(self): print("Old") A.show = lambda self: print("New")
Tip:
Say it carefully—rare in production.
Q68. What is serialization vs deserialization?
Explanation:
Serialization converts object to storage format.
Deserialization restores it back.
Used in APIs and ML.
JSON and pickle common.
Important in distributed systems.
Example:
import json x = json.dumps({"a":1}) print(json.loads(x))
Tip:
JSON for APIs, Pickle for Python objects.
Q69. What is asyncio?
Explanation:
asyncio enables asynchronous programming.
Used for concurrent I/O operations.
Uses async/await syntax.
Improves performance for network apps.
Common in microservices.
Example:
import asyncio async def main(): print("Hello") asyncio.run(main())
Tip:
Async is for I/O concurrency.
Q70. What is metaclass?
Explanation:
Metaclass defines class behavior.
Class of a class.
Rarely used directly.
Used in frameworks like Django.
Advanced Python topic.
Example:
class Meta(type): pass class A(metaclass=Meta): pass
Tip:
Say: metaclass creates classes.
Q71. What is slot in Python?
Explanation:
slots restricts attributes.
Reduces memory usage.
Improves performance.
Used in large systems.
Prevents dynamic attributes.
Example:
class A: __slots__ = ['x']
Tip:
Slots optimize memory.
Q72. What is heapq?
Explanation:
heapq implements priority queue.
Always pops smallest element.
Used in scheduling.
Fast O(log n).
Common algorithm question.
Example:
import heapq h=[3,1,2] heapq.heapify(h) print(heapq.heappop(h))
Tip:
Heap = priority queue.
Q73. What is bisect?
Explanation:
bisect maintains sorted order.
Used in binary search.
Very fast insertion.
Useful in ranking systems.
Avoids manual searching.
Example:
import bisect a=[1,3,5] bisect.insort(a,4)
Tip:
bisect = binary insert.
Q74. What is traceback?
Explanation:
Traceback shows error path.
Helps debugging.
Displays call stack.
Used in logging.
Critical for production errors.
Example:
import traceback
Tip:
Traceback explains crash.
Q75. What is profiling?
Explanation:
Profiling finds slow code.
Measures execution time.
Used for optimization.
cProfile is common.
Important for performance tuning.
Example:
import cProfile
Tip:
Profiling before optimizing.
Q76. What is memory leak?
Explanation:
Memory leak means unused memory not released.
Occurs with circular references.
Leads to crashes.
gc module helps.
Critical in long-running apps.
Example:
import gc
Tip:
Mention garbage collector.
Q77. What is unit testing?
Explanation:
Unit testing validates individual components.
Ensures code quality.
Uses unittest or pytest.
Mandatory in MNCs.
Part of CI/CD.
Example:
import unittest
Tip:
Testing improves reliability.
Q78. What is mocking?
Explanation:
Mocking simulates real objects.
Used in testing.
Avoids external dependencies.
Speeds tests.
Uses unittest.mock.
Example:
from unittest import mock
Tip:
Mock replaces real services.
Q79. What is CI/CD in Python projects?
Explanation:
CI = continuous integration.
CD = continuous deployment.
Automates testing and release.
Used with GitHub Actions/Jenkins.
Standard in enterprises.
Example:
# pipeline runs tests automatically
Tip:
Mention automation.
Q80. How is Python used in Data Science pipelines?
Explanation:
Python handles data ingestion.
Performs cleaning and modeling.
Supports visualization.
Deploys models via APIs.
Dominant DS language.
Example:
import pandas as pd import numpy as np
Tip:
Explain full pipeline flow.