Fundamentals Interview Questions
Comprehensive fundamentals interview questions and answers for Python. Prepare for your next job interview with expert guidance.
Questions Overview
1. What are the key features of Python?
Basic2. How does Python handle memory management?
Moderate3. Explain the difference between lists and tuples in Python.
Basic4. What are Python's built-in data types?
Basic5. How do you create a virtual environment in Python?
Moderate6. What is the purpose of the `__init__` method in Python classes?
Basic7. Explain list comprehensions and provide an example.
Moderate8. How does Python's garbage collection work?
Advanced9. What are decorators in Python and how are they used?
Advanced10. How can you handle exceptions in Python?
Moderate1. What are the key features of Python?
BasicPython is a high-level, interpreted programming language known for its readability, simplicity, and versatility. Key features include dynamic typing, automatic memory management, an extensive standard library, support for multiple programming paradigms (procedural, object-oriented, functional), and a large ecosystem of third-party packages.
2. How does Python handle memory management?
ModeratePython uses automatic memory management through a private heap containing all Python objects and data structures. It employs reference counting for garbage collection and a cyclic garbage collector to detect and collect reference cycles, ensuring efficient memory usage without manual intervention.
3. Explain the difference between lists and tuples in Python.
BasicLists are mutable, allowing modifications like adding, removing, or changing elements. Tuples are immutable, meaning once created, their elements cannot be altered. Lists use square brackets [ ], while tuples use parentheses ( ). Tuples can be used as keys in dictionaries due to their immutability.
4. What are Python's built-in data types?
BasicPython's built-in data types include numeric types (int, float, complex), sequence types (list, tuple, range), text type (str), mapping type (dict), set types (set, frozenset), boolean type (bool), and binary types (bytes, bytearray, memoryview).
5. How do you create a virtual environment in Python?
ModerateYou can create a virtual environment using the `venv` module by running `python -m venv myenv`, where `myenv` is the name of the environment. Activate it with `source myenv/bin/activate` on Unix or `myenv\Scripts\activate` on Windows, isolating dependencies for your project.
6. What is the purpose of the `__init__` method in Python classes?
BasicThe `__init__` method is the constructor in Python classes. It initializes the instance attributes of a class when a new object is created, setting up the initial state of the object.
7. Explain list comprehensions and provide an example.
ModerateList comprehensions provide a concise way to create lists by iterating over an iterable and optionally including conditional logic. Example: `[x**2 for x in range(10) if x % 2 == 0]` creates a list of squares of even numbers from 0 to 9.
8. How does Python's garbage collection work?
AdvancedPython uses reference counting to keep track of the number of references to each object. When an object's reference count drops to zero, it's immediately deallocated. Additionally, a cyclic garbage collector handles reference cycles by periodically identifying and collecting objects involved in cycles.
9. What are decorators in Python and how are they used?
AdvancedDecorators are functions that modify or enhance other functions or methods without changing their code. They are applied using the `@decorator_name` syntax above a function definition. Common uses include logging, access control, and memoization.
10. How can you handle exceptions in Python?
ModerateExceptions are handled using `try` and `except` blocks. Code that may raise an exception is placed inside the `try` block, and the `except` block catches and handles specific exceptions. Optionally, `finally` can be used to execute code regardless of whether an exception occurred.