Premium
Top 10 Python Functions Interview Questions
Ace Python functions interview questions with 10 essential topics: mutable default argument pitfalls, closure variable capture in loops, decorator patterns (function and class-based), lambda scoping rules, args and kwargs unpacking mechanics, function annotations and type hints, recursion limits, the diamond problem in multiple inheritance with MRO, function caching with lru_cache, and contextlib utilities for resource management.
| Question No. | Question |
|---|---|
| 1 | What is aliasing? |
| 2 | What is garbage collection? |
| 3 | What is mutability and why is it dangerous in certain scenarios? |
| 4 | What is cloning? |
| 5 | Differentiate between deep and shallow copies |
| 6 | How nested lists are stored in memory? |
| 7 | How strings are stored in memory |
| 8 | Why tuples take less memory than lists? |
| 9 | How set index position is decided? |
| 10 | Why mutable types are not allowed in sets/dicts |
Understanding variable aliasing and memory addresses in Python
Explanation
- Variable aliasing occurs when multiple variables reference the same memory location, as demonstrated by assigning
b = aaftera = 4 - The
id()function returns the unique identity of an object in memory, which remains constant during the object's lifetime - Memory addresses are displayed in both decimal and hexadecimal formats using
hex(id())to show the underlying memory location - When variables reference the same value, they share the same memory address, proving that Python optimizes memory usage through aliasing
- This behavior demonstrates Python's object model where variables are references to objects rather than containers holding values
python
Output
text
Python del statement behavior and variable reference deletion
Explanation
- The del statement removes a variable reference from the namespace but does not immediately free the memory occupied by the object it referenced
- In the code snippet, del a removes the variable name 'a' from the local scope while the underlying object remains in memory if other references exist
- Attempting to print the variable after deletion results in a NameError because the variable name is no longer bound to any object
- This demonstrates Python's reference counting garbage collection mechanism where objects persist until their reference count reaches zero
- The del statement is useful for explicitly removing variable bindings to prevent accidental use of stale data or to free up namespace space
python
Output
text
Python variable reference and memory address display using print and id functions
Explanation
- The code prints the value of variable b using the print() function, which outputs whatever data is stored in that variable
- The code displays the memory address of variable b using the id() function, which returns the unique identifier for the object in memory
- This demonstrates how Python variables act as references to objects rather than containers holding values directly
- The id() function helps understand memory management and object identity in Python's memory model
- Together these statements show both the content and location of a variable's data in memory
python
Output
text
Understanding variable assignment and reference counting in Python memory management
Explanation
- The variable
ais assigned a string value 'AI 2026-27', creating a reference to that string in memory. - The variable
bis then assigned the value ofa, which meansbnow references the same string object asa. - Similarly,
cis assigned the value ofb, makingcanother reference to the same string object. - This code snippet demonstrates how multiple variables can reference the same object in memory, which is crucial for understanding Python's memory management and garbage collection.
- If all references to the string 'AI 2026-27' are deleted or go out of scope, Python's garbage collector will reclaim that memory.
python
This code retrieves the reference count of a specific string in Python's memory management.
Explanation
- The
sysmodule is imported to access system-specific parameters and functions. - The
getrefcountfunction returns the reference count of the specified object, which in this case is the string 'AI 2026-27'. - The reference count indicates how many references point to the object in memory, helping to understand memory management and garbage collection.
- The comment suggests that there are three variables referencing this string, which would affect its reference count.
python
Output
text
Retrieve the current garbage collection threshold in Python using the gc module
Explanation
- The code imports the built-in
gcmodule, which provides access to the garbage collection facilities in Python. - It calls the
get_threshold()function from thegcmodule, which returns the current threshold values for garbage collection. - The threshold indicates when the garbage collector will run, based on the number of object allocations and deallocations.
- The output of the
printstatement will display the threshold values as a tuple, helping developers understand the garbage collection behavior in their applications.
python
Output
text
This code snippet demonstrates manual memory management in Python using garbage collection.
Explanation
Unlock the Full Article
You're reading a free 30% preview. The rest of this article is available to premium members.
Already a premium member? Sign in here
