Capgemini is one of the leading technology companies, and Python is widely used in fields like web development, data science, and automation. To help you prepare for your Python Developer role at Capgemini, here are the Top 20 Python Interview Questions along with their answers.
1. What are the key features of Python?
Python is an interpreted, high-level, dynamically-typed programming language with features like simplicity, readability, portability, and support for both OOP and functional programming.
2. How does Python handle memory management?
Python has an inbuilt garbage collector, which manages memory by deallocating objects that are no longer in use. It uses reference counting and a cyclic garbage collector for memory management.
3. What are Python namespaces?
Namespaces in Python are containers that hold names (variables and functions) and map them to objects. There are four types: built-in, global, local, and enclosed namespaces.
4. What is PEP 8, and why is it important?
PEP 8 is Python’s style guide for writing clean, readable code. It suggests guidelines for indentation, naming conventions, line length, and comments.
5. What is the difference between lists and tuples in Python?
- Lists: Mutable, meaning they can be changed after creation.
- Tuples: Immutable, meaning they cannot be modified once created.
6. What are Python decorators?
Decorators are a way to modify or enhance the behavior of a function or class without permanently modifying it. They are defined using the @
symbol before a function definition.
7. Explain list comprehension with an example.
List comprehension is a concise way to create lists using an expression and a loop. For example:
squares = [x**2 for x in range(5)]
8. What is the purpose of self
in Python?
self
is a reference to the current instance of a class. It is used to access the class variables and methods from within the class.
9. What is the difference between __init__
and __new__
methods?
__init__
: Initializes an instance after it is created. It is the constructor of a class.__new__
: Creates a new instance of a class and is called before__init__
.
10. What are the different types of inheritance in Python?
Python supports:
- Single inheritance
- Multiple inheritance
- Multilevel inheritance
- Hierarchical inheritance
- Hybrid inheritance
11. How does exception handling work in Python?
Exceptions in Python are handled using try
, except
, else
, and finally
blocks. The try
block contains the code that might throw an exception, and the except
block handles it.
12. What is the difference between append()
and extend()
in Python?
append()
: Adds an element to the end of a list.extend()
: Adds multiple elements (from another iterable) to the end of a list.
13. How is multithreading achieved in Python?
Multithreading in Python is achieved using the threading
module. However, due to Python’s Global Interpreter Lock (GIL), threads cannot run in parallel in CPU-bound tasks, but they are useful for I/O-bound tasks.
14. What are Python generators?
Generators are a type of iterable that return items lazily, meaning they generate values one at a time and only when required. They are created using yield
instead of return
in a function.
15. What is the difference between shallow copy and deep copy in Python?
- Shallow copy: Creates a new object but copies references of nested objects.
- Deep copy: Creates a new object and recursively copies all objects, including nested ones, using the
copy
module.
16. Explain the use of with
statement in Python.
The with
statement simplifies exception handling by automatically managing resources, such as file streams. It ensures proper resource cleanup, such as closing a file after reading.
with open('file.txt', 'r') as file:
content = file.read()
17. What is a lambda function in Python?
A lambda function is a small, anonymous function defined using the lambda
keyword. It can have any number of arguments but only one expression. For example:
square = lambda x: x**2
18. How do you perform static type checking in Python?
Python 3.5 introduced type hints that allow static type checking. You can specify types using function annotations, and tools like mypy
can check type correctness.
def add(x: int, y: int) -> int:
return x + y
19. What is the difference between sort()
and sorted()
?
sort()
: Modifies the list in place.sorted()
: Returns a new sorted list from the original iterable without modifying it.
20. How do you implement data structures like stacks and queues in Python?
- Stack: Implemented using lists with
append()
to push andpop()
to remove. - Queue: Implemented using the
deque
class from thecollections
module for efficient FIFO operations.
Conclusion
These 20 Python interview questions are essential for preparing for a Capgemini Python Developer interview. Be sure to practice writing clean, efficient Python code and brush up on concepts like OOP, data structures, and Python’s built-in modules.
Good luck with your interview preparation!
Add a comment: