Top 10 Interview Questions on Python
1. What is the difference between Python 2 and Python 3?
Answer: Python 2 and Python 3 are different versions of the Python programming language. Python 3 introduced several changes to improve the language's design and fix inconsistencies. Some notable changes include print function (Python 3), Unicode support, and division behavior.
2. Explain the Global Interpreter Lock (GIL) in Python.
Answer: The Global Interpreter Lock is a mutex that prevents multiple native threads from executing Python bytecodes in parallel in a single process. This means that only one thread can execute Python code at a time, limiting the utilization of multi-core processors in CPU-bound tasks.
3. What are decorators in Python?
Answer: Decorators are a powerful feature in Python that allow you to modify or extend the behavior of functions or methods. They are often used for tasks like logging, authentication, and caching. Decorators are applied using the "@" symbol before a function definition.
4. How does memory management work in Python?
Answer: Python uses a private heap space to manage memory. Objects are created and stored in this heap, and a reference count mechanism keeps track of when an object is no longer needed. The memory manager deallocates objects when their reference count drops to zero.
5. Explain the differences between a list and a tuple in Python.
Answer: Lists are mutable, ordered collections of items, while tuples are immutable ordered collections. Lists are defined using square brackets `[ ]`, and tuples are defined using parentheses `( )`. Lists support operations like appending and removing items, while tuples do not allow these modifications.
6. What are "list comprehensions" in Python?
Answer: List comprehensions are a concise way to create lists in Python. They allow you to generate a new list by applying an expression to each item in an existing iterable (such as a list, tuple, or range).
7. How can you handle exceptions in Python?
Answer: In Python, you can use try-except blocks to handle exceptions. Code that might raise an exception is placed inside the try block, and the corresponding exception-handling code is placed inside the except block. This prevents the program from crashing and allows for graceful error handling.
8. Explain the concept of "duck typing" in Python.
Answer: Duck typing is a dynamic typing concept in which the type or class of an object is determined by its behavior (methods and properties) rather than its explicit type. If an object walks like a duck and quacks like a duck, it's treated as a duck.
9. What is the purpose of the "__init__" method in Python classes?
Answer: The `__init__` method is a special method in Python classes. It's used to initialize instance variables and perform any setup that's required when creating an object from a class.
10. How does Python support "inheritance"?
Answer: Inheritance is a fundamental concept in object-oriented programming, and Python supports it through class inheritance. A subclass can inherit attributes and methods from a superclass, allowing for code reuse and specialization.