0% found this document useful (0 votes)
113 views2 pages

Python Interview Questions Overview

Uploaded by

sachinverma6975
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views2 pages

Python Interview Questions Overview

Uploaded by

sachinverma6975
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

What are Python’s key features?

Difference between Python 2 and Python 3.

Why is Python called an interpreted language?

What are Python’s built-in data types?

Difference between mutable and immutable objects.

Difference between is and ==.

Use of id(), type(), and dir().

How does Python manage memory?

What is dynamic typing?

Explain garbage collection in Python.

Difference between list, tuple, set, and dictionary.

How to remove duplicates from a list.

Dictionary comprehension example.

Shallow copy vs deep copy.

How to merge two dictionaries in Python 3.9+.

Flatten a list of lists in Python.

How dictionaries handle hash collisions.

Difference between append() and extend().

How to implement a stack and queue.

What is frozenset and its uses.

Default and keyword arguments in functions.

Use of *args and **kwargs.

What are lambda functions?

Explain closures with example.

What are decorators in Python?

Difference between class method, static method, and instance method.

What is multiple inheritance?

Explain Method Resolution Order (MRO).

Difference between __str__ and __repr__.

How to implement operator overloading.


Difference between modules and packages.

How to create a virtual environment.

Difference between pip and conda.

How Python’s import system works.

Absolute vs relative imports.

Creating and using custom exceptions.

Example of Python logging module.

Built-in modules for concurrency.

Difference between multiprocessing and threading.

Use of the subprocess module.

What is Global Interpreter Lock (GIL)?

How Python manages memory references.

Difference between generators and iterators.

Use of yield with example.

Synchronous vs asynchronous programming.

How asyncio works.

What are context managers and with statement?

What are metaclasses in Python?

Explain duck typing.

What are type hints and their benefits?

Common questions

Powered by AI

Duck typing is a concept in Python that allows for dynamic determination of an object's suitability for a task based on its behavior (methods and properties), rather than its specific class or interface. This means that as long as an object implements the requisite method(s) called in the code, it can be used in that role, regardless of its actual type, adhering to the saying, "If it looks like a duck and quacks like a duck, it must be a duck." Duck typing benefits software design by promoting flexibility and ease of code reuse, as functions or classes operate on any object that provides the expected behavior, reducing dependency on specific class hierarchies and promoting generic programming .

The Global Interpreter Lock (GIL) is a mutex in Python that prevents multiple native threads from executing Python bytecodes simultaneously in a single interpreter. This is necessary because CPython, the main implementation of Python, does not support true multi-core execution due to non-thread-safe memory management features. While this simplifies memory management and allows for safe multithreading in I/O-bound applications, it severely limits performance in CPU-bound applications, as threads cannot run in parallel on multi-core processors. To bypass GIL limitations, developers often use the multiprocessing module or external libraries like 'concurrent.futures' .

The 'yield' keyword in Python is used within generator functions to produce a series of values over time, which can be iterated over one at a time. Unlike a traditional return statement, which exits the function and returns a single value, 'yield' allows the function's execution state to be saved, enabling the function to be resumed right where it left off. This approach is memory efficient for large sequences or streaming data, as it does not require all values to be stored in memory at once. It supports lazy evaluation and enables the creation of infinite sequences, making it ideal for use cases like producing dynamically composed data sequences or processing data pipelines .

Context managers in Python are used to properly manage resources, such as files or network connections, ensuring they are acquired and released as necessary. The 'with' statement is used to wrap the execution of a block of code within methods defined by a context manager. It handles the opening and closing of resources automatically, which prevents resource leaks and simplifies code by eliminating the need for explicit resource cleanup using try-finally blocks. Context managers implement '__enter__' and '__exit__' methods that define the code executed before and after the block inside the 'with' statement, making them ideal for managing cleanup operations .

Python's built-in collections—list, tuple, set, and dictionary—differ in their properties and use cases. Lists are mutable ordered collections, ideal for storing ordered data with frequent updates. Tuples, unlike lists, are immutable ordered collections, suitable for fixed data, ensuring hashable storage, such as keys in a dictionary. Sets are unordered collections that do not allow duplicate elements, making them useful for membership tests and operations like intersection, union, and difference. Dictionaries are collections of key-value pairs, optimized for rapid data retrieval based on keys. They are ideal when there is a need for a strong mapping between sets of unique keys and corresponding values .

In Python, a shallow copy only copies the top-level data structure, meaning nested elements are still references to the objects contained in the original structure. This can be implemented using the 'copy()' method or the 'copy' module's 'copy()' function. Conversely, a deep copy creates copies of the top-level and all nested levels, so changes in nested structures do not affect the original. Deep copies can be implemented using the 'deepcopy()' function from the 'copy' module. While shallow copies are faster and use less memory, they can lead to unintended mutations of nested objects, whereas deep copies prevent such mutations at the cost of additional memory usage and processing time .

In Python, 'is' and '==' are both operators used to compare values, but they serve different purposes. The 'is' operator compares the identities of two objects, checking whether they refer to the exact same object in memory. In contrast, the '==' operator compares the values of two objects, checking if they are equivalent in terms of content. 'is' is often used for singleton objects like None, where identity is more important than value, while '==' is used to check equality of values across iterable and custom objects .

The primary differences between Python 2 and Python 3 include changes in syntax and libraries. Python 3 has improved Unicode support as strings are Unicode by default, while Python 2 has ASCII-only strings unless explicitly defined with a 'u' prefix. The 'print' statement in Python 2 is now a 'print()' function in Python 3. In Python 3, the integer division behavior changed, using a single '/' now results in a floating-point result instead of an integer. Additionally, Python 3 introduced keyword-only arguments and a host of other optimization and library updates, such as changes in the standard library modules and functions .

Python handles dynamic typing by ensuring that each variable name is just a reference to an object stored in memory. The memory management system is responsible for allocating and deallocating memory on the fly using the Python memory manager for objects like lists, dictionaries, and custom classes. Python's garbage collector automatically handles deallocation of memory for unneeded objects, helping to manage memory effectively. This dynamic nature allows objects of different types to be associated with the same variable, offering flexibility in programming .

Decorators in Python are a design pattern that allows programmers to modify or enhance functions or methods. They are typically defined as functions that take another function as an argument and return a new function with added functionality. Decorators are applied using the '@' syntax above the target function definition. They enable the augmentation of functions in a clean, readable way without permanently modifying the original function. This is useful for functionalities like logging, access control, memoization, and more. By allowing function behavior enhancement without altering the function code, decorators promote code reuse and separation of concerns .

You might also like