Python Interview Questions and Answers

1. What is Python’s Global Interpreter Lock (GIL)?

A Global Interpreter Key is a mutex that protects Python object access and prevents multiple threads from executing Python bytecodes at the same time. This can limit thread parallelism, affecting the performance of multi-threaded Python programs.

2. In Python, what is the difference between __str__ and __repr__?

 Both methods are used to represent objects as strings, but __str__ is for end users and should be readable, whereas __repr__ is for programmers and should provide an unambiguous representation of the object.

3. What is the distinction between shallow and deep copy?

Shallow copy generates a new object, but not for nested structures; it refers to the same objects. A deep copy makes an entirely independent copy of the original object and all objects contained within it.

4. Describe how decorations are used in Python.

Decorators are a versatile and powerful way to change or extend the behavior of functions or methods. They can be reached via the @decorator syntax and allow you to wrap or modify their decorator function.

5. How does garbage collection work in Python?

Python manages memory by combining comparison counting and loop detection. When an object’s reference count reaches zero, it is freed. Objects involved in reference cycles are detected and collected by the period detector.

6. What does the if __name__ == andquot;__main__andquot;: statement do?

It determines whether the Python script is being run as a main program or as a module. This block’s code is only executed when the script is run directly.

7. How does Python’s super() function function?

super() is used to invoke a base class method. It returns a temporary object of the superclass from which you can invoke its methods. This is usually used in conjunction with the hereditary dominance method.

8. What are Python metaclasses?

Metaclasses are classes within classes. They specify how classes are created and how they behave. Classes are instances of the metaclass type by default, but you can customize class creation by creating your own metaclasses.

9. Explain Python’s Global Interpreter Key (GIL) and its impact on multithreading.

The GIL is a mutex that protects access to Python objects and prevents multiple native threads from executing Python bytecode at the same time. This can limit the performance of multi-threaded Python programs, especially for CPU-intensive tasks because only one thread can execute Python bytecode at a time.

10. What is the purpose of async and await keywords in Python?

These keywords are used in asynchronous programming to define and await asynchronous coroutines. They allow code to run without blocking, allowing other tasks to be performed while waiting for I/O operations to complete.

These questions cover a variety of advanced Python topics and can be used to gauge a developer’s deep understanding of the language.

Leave a Comment

Your email address will not be published. Required fields are marked *