|
| 1 | +# Python Interview Questions (September 2025) |
| 2 | + |
| 3 | +This document contains 25 frequently asked Python interview questions from major tech companies in **September 2025**. |
| 4 | +Each question includes an **answer, explanation, and code example (where applicable)**. |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## 1. What are Python’s key features? |
| 9 | +**Answer:** Python is interpreted, dynamically typed, object-oriented, and has extensive libraries. |
| 10 | +**Explanation:** These features make Python versatile for scripting, web development, ML, and system tasks. |
| 11 | +**Code Example:** |
| 12 | +```python |
| 13 | +print("Hello, Python!") # Interpreted, no compilation needed |
| 14 | +``` |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## 2. What is PEP 8 and why is it important? |
| 19 | +**Answer:** PEP 8 is the style guide for writing clean, readable Python code. |
| 20 | +**Explanation:** Following PEP 8 improves consistency and readability across projects. |
| 21 | +**Code Example:** |
| 22 | +```python |
| 23 | +# Good PEP 8 practice |
| 24 | +def add_numbers(a, b): |
| 25 | + return a + b |
| 26 | +``` |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## 3. Explain Python’s memory management. |
| 31 | +**Answer:** Python uses reference counting and garbage collection for memory management. |
| 32 | +**Explanation:** Objects are destroyed when reference count reaches zero. Cyclic references are handled by GC. |
| 33 | +**Code Example:** |
| 34 | +```python |
| 35 | +import gc |
| 36 | +print(gc.isenabled()) # True by default |
| 37 | +``` |
| 38 | + |
| 39 | +--- |
| 40 | + |
| 41 | +## 4. What is the Global Interpreter Lock (GIL)? |
| 42 | +**Answer:** The GIL ensures only one thread executes Python bytecode at a time. |
| 43 | +**Explanation:** Good for memory safety, but limits multi-core CPU-bound performance. |
| 44 | +**Code Example:** |
| 45 | +```python |
| 46 | +import threading |
| 47 | + |
| 48 | +x = 0 |
| 49 | +def task(): |
| 50 | + global x |
| 51 | + for _ in range(1000000): |
| 52 | + x += 1 |
| 53 | + |
| 54 | +t1 = threading.Thread(target=task) |
| 55 | +t2 = threading.Thread(target=task) |
| 56 | +t1.start(); t2.start() |
| 57 | +t1.join(); t2.join() |
| 58 | +print(x) # Not 2M due to GIL |
| 59 | +``` |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +## 5. Difference between deep copy and shallow copy? |
| 64 | +**Answer:** Shallow copy copies references; deep copy creates independent copies. |
| 65 | +**Explanation:** Shallow copies share nested objects, deep copies don’t. |
| 66 | +**Code Example:** |
| 67 | +```python |
| 68 | +import copy |
| 69 | +a = [[1,2],[3,4]] |
| 70 | +shallow = copy.copy(a) |
| 71 | +deep = copy.deepcopy(a) |
| 72 | +a[0][0] = 99 |
| 73 | +print(shallow) # [[99, 2], [3, 4]] |
| 74 | +print(deep) # [[1, 2], [3, 4]] |
| 75 | +``` |
| 76 | + |
| 77 | +--- |
| 78 | + |
| 79 | +## 6. Explain Python’s `with` statement. |
| 80 | +**Answer:** It simplifies resource management using context managers. |
| 81 | +**Explanation:** Ensures resources are released properly. |
| 82 | +**Code Example:** |
| 83 | +```python |
| 84 | +with open("file.txt", "w") as f: |
| 85 | + f.write("Hello!") |
| 86 | +# File auto-closed |
| 87 | +``` |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +## 7. What are Python decorators? |
| 92 | +**Answer:** Functions that modify behavior of other functions. |
| 93 | +**Explanation:** Useful for logging, authentication, caching. |
| 94 | +**Code Example:** |
| 95 | +```python |
| 96 | +def log(func): |
| 97 | + def wrapper(*args): |
| 98 | + print("Calling", func.__name__) |
| 99 | + return func(*args) |
| 100 | + return wrapper |
| 101 | + |
| 102 | +@log |
| 103 | +def greet(name): |
| 104 | + print(f"Hello {name}") |
| 105 | + |
| 106 | +greet("Python") |
| 107 | +``` |
| 108 | + |
| 109 | +--- |
| 110 | + |
| 111 | +## 8. What are Python generators? |
| 112 | +**Answer:** Functions using `yield` to return values lazily. |
| 113 | +**Explanation:** Memory efficient, used in large datasets. |
| 114 | +**Code Example:** |
| 115 | +```python |
| 116 | +def gen(): |
| 117 | + for i in range(3): |
| 118 | + yield i |
| 119 | + |
| 120 | +for x in gen(): |
| 121 | + print(x) |
| 122 | +``` |
| 123 | + |
| 124 | +--- |
| 125 | + |
| 126 | +## 9. Difference between `is` and `==`? |
| 127 | +**Answer:** `is` checks identity, `==` checks equality. |
| 128 | +**Explanation:** Two objects can be equal but not identical. |
| 129 | +**Code Example:** |
| 130 | +```python |
| 131 | +a = [1,2] |
| 132 | +b = [1,2] |
| 133 | +print(a == b) # True |
| 134 | +print(a is b) # False |
| 135 | +``` |
| 136 | + |
| 137 | +--- |
| 138 | + |
| 139 | +## 10. What are Python’s data structures? |
| 140 | +**Answer:** Lists, tuples, sets, dicts. |
| 141 | +**Explanation:** Built-ins cover most needs, implemented efficiently. |
| 142 | +**Code Example:** |
| 143 | +```python |
| 144 | +my_list = [1,2,3] |
| 145 | +my_tuple = (1,2,3) |
| 146 | +my_set = {1,2,3} |
| 147 | +my_dict = {"a":1,"b":2} |
| 148 | +``` |
| 149 | + |
| 150 | +--- |
| 151 | + |
| 152 | +## 11. Explain list comprehension. |
| 153 | +**Answer:** A concise way to create lists. |
| 154 | +**Explanation:** More readable and efficient than loops. |
| 155 | +**Code Example:** |
| 156 | +```python |
| 157 | +squares = [x**2 for x in range(5)] |
| 158 | +print(squares) |
| 159 | +``` |
| 160 | + |
| 161 | +--- |
| 162 | + |
| 163 | +## 12. Explain Python’s `@staticmethod` vs `@classmethod`. |
| 164 | +**Answer:** |
| 165 | +- `staticmethod`: No access to class or instance. |
| 166 | +- `classmethod`: Accesses class but not instance. |
| 167 | +**Code Example:** |
| 168 | +```python |
| 169 | +class MyClass: |
| 170 | + @staticmethod |
| 171 | + def sm(): |
| 172 | + print("Static") |
| 173 | + @classmethod |
| 174 | + def cm(cls): |
| 175 | + print("Class", cls) |
| 176 | + |
| 177 | +MyClass.sm() |
| 178 | +MyClass.cm() |
| 179 | +``` |
| 180 | + |
| 181 | +--- |
| 182 | + |
| 183 | +## 13. What is Python’s MRO? |
| 184 | +**Answer:** Method Resolution Order defines order of class inheritance search. |
| 185 | +**Explanation:** Uses C3 linearization. |
| 186 | +**Code Example:** |
| 187 | +```python |
| 188 | +class A: pass |
| 189 | +class B(A): pass |
| 190 | +class C(B): pass |
| 191 | +print(C.mro()) |
| 192 | +``` |
| 193 | + |
| 194 | +--- |
| 195 | + |
| 196 | +## 14. What are Python descriptors? |
| 197 | +**Answer:** Objects defining `__get__`, `__set__`, `__delete__`. |
| 198 | +**Explanation:** Used for properties, ORM fields. |
| 199 | +**Code Example:** |
| 200 | +```python |
| 201 | +class Descriptor: |
| 202 | + def __get__(self, obj, objtype): |
| 203 | + return "value" |
| 204 | +class My: |
| 205 | + x = Descriptor() |
| 206 | + |
| 207 | +print(My().x) |
| 208 | +``` |
| 209 | + |
| 210 | +--- |
| 211 | + |
| 212 | +## 15. How does Python handle multithreading vs multiprocessing? |
| 213 | +**Answer:** |
| 214 | +- **Multithreading:** Limited by GIL for CPU tasks. Good for I/O. |
| 215 | +- **Multiprocessing:** Spawns processes, bypassing GIL. |
| 216 | +**Code Example:** |
| 217 | +```python |
| 218 | +from multiprocessing import Process |
| 219 | + |
| 220 | +def work(): |
| 221 | + print("Working") |
| 222 | + |
| 223 | +p = Process(target=work) |
| 224 | +p.start(); p.join() |
| 225 | +``` |
| 226 | + |
| 227 | +--- |
| 228 | + |
| 229 | +## 16. Explain Python’s garbage collection. |
| 230 | +**Answer:** Uses reference counting + cyclic GC. |
| 231 | +**Explanation:** Cyclic GC detects and removes circular references. |
| 232 | +**Code Example:** |
| 233 | +```python |
| 234 | +import gc |
| 235 | +gc.collect() |
| 236 | +``` |
| 237 | + |
| 238 | +--- |
| 239 | + |
| 240 | +## 17. What is monkey patching? |
| 241 | +**Answer:** Dynamically modifying a class/module at runtime. |
| 242 | +**Explanation:** Can be useful but risky. |
| 243 | +**Code Example:** |
| 244 | +```python |
| 245 | +class A: |
| 246 | + def hello(self): print("Hi") |
| 247 | + |
| 248 | +def new_hello(self): print("Hello patched") |
| 249 | + |
| 250 | +A.hello = new_hello |
| 251 | +A().hello() |
| 252 | +``` |
| 253 | + |
| 254 | +--- |
| 255 | + |
| 256 | +## 18. Difference between `@property` and normal methods? |
| 257 | +**Answer:** `@property` allows method access like attributes. |
| 258 | +**Explanation:** Used for encapsulation. |
| 259 | +**Code Example:** |
| 260 | +```python |
| 261 | +class Circle: |
| 262 | + def __init__(self, r): self.r = r |
| 263 | + @property |
| 264 | + def area(self): return 3.14 * self.r**2 |
| 265 | + |
| 266 | +c = Circle(5) |
| 267 | +print(c.area) |
| 268 | +``` |
| 269 | + |
| 270 | +--- |
| 271 | + |
| 272 | +## 19. Explain Python’s `__slots__`. |
| 273 | +**Answer:** Restricts attribute creation, saves memory. |
| 274 | +**Explanation:** Used in memory-sensitive apps. |
| 275 | +**Code Example:** |
| 276 | +```python |
| 277 | +class Point: |
| 278 | + __slots__ = ['x','y'] |
| 279 | + def __init__(self,x,y): self.x,self.y=x,y |
| 280 | +``` |
| 281 | + |
| 282 | +--- |
| 283 | + |
| 284 | +## 20. Explain Python’s AsyncIO. |
| 285 | +**Answer:** Library for async programming with `async/await`. |
| 286 | +**Explanation:** Efficient for I/O-bound tasks. |
| 287 | +**Code Example:** |
| 288 | +```python |
| 289 | +import asyncio |
| 290 | + |
| 291 | +async def hello(): |
| 292 | + await asyncio.sleep(1) |
| 293 | + print("Hello async") |
| 294 | + |
| 295 | +asyncio.run(hello()) |
| 296 | +``` |
| 297 | + |
| 298 | +--- |
| 299 | + |
| 300 | +## 21. How does Python implement immutability? |
| 301 | +**Answer:** Immutable types: str, tuple, frozenset. |
| 302 | +**Explanation:** Values cannot be modified in-place. |
| 303 | +**Code Example:** |
| 304 | +```python |
| 305 | +s = "abc" |
| 306 | +# s[0] = "x" # Error |
| 307 | +``` |
| 308 | + |
| 309 | +--- |
| 310 | + |
| 311 | +## 22. Explain Python’s metaclasses. |
| 312 | +**Answer:** Classes of classes, control class creation. |
| 313 | +**Explanation:** Used in frameworks/ORMs. |
| 314 | +**Code Example:** |
| 315 | +```python |
| 316 | +class Meta(type): |
| 317 | + def __new__(cls, name, bases, dct): |
| 318 | + print("Creating", name) |
| 319 | + return super().__new__(cls, name, bases, dct) |
| 320 | + |
| 321 | +class My(metaclass=Meta): pass |
| 322 | +``` |
| 323 | + |
| 324 | +--- |
| 325 | + |
| 326 | +## 23. Difference between `copy` and `deepcopy`? |
| 327 | +**Answer:** `copy` keeps refs, `deepcopy` copies recursively. |
| 328 | +(Already explained in Q5 for clarity, but here reinforced as it’s frequently asked.) |
| 329 | + |
| 330 | +--- |
| 331 | + |
| 332 | +## 24. Explain duck typing. |
| 333 | +**Answer:** Behavior determined by methods, not inheritance. |
| 334 | +**Explanation:** “If it quacks like a duck, it’s a duck.” |
| 335 | +**Code Example:** |
| 336 | +```python |
| 337 | +class Duck: |
| 338 | + def quack(self): print("Quack") |
| 339 | +class Person: |
| 340 | + def quack(self): print("I’m quacking like a duck") |
| 341 | + |
| 342 | +def make_quack(obj): obj.quack() |
| 343 | + |
| 344 | +make_quack(Duck()) |
| 345 | +make_quack(Person()) |
| 346 | +``` |
| 347 | + |
| 348 | +--- |
| 349 | + |
| 350 | +## 25. Explain Python’s typing module. |
| 351 | +**Answer:** Provides type hints for static analysis. |
| 352 | +**Explanation:** Helps with readability and catching errors. |
| 353 | +**Code Example:** |
| 354 | +```python |
| 355 | +from typing import List |
| 356 | + |
| 357 | +def square(nums: List[int]) -> List[int]: |
| 358 | + return [x*x for x in nums] |
| 359 | +``` |
| 360 | + |
| 361 | +--- |
| 362 | + |
0 commit comments