Skip to content
Premium

Python Interview Questions & Answers Guide

Master Python technical interviews with 25 essential questions covering memory management, mutable vs immutable objects, shallow vs deep copy, the GIL and multithreading, decorators with arguments, closures, generators, iterators, context managers, asyncio concurrency, and advanced OOP topics including MRO, descriptors, and metaclasses.

Question No.Question
1What is aliasing?
2What is garbage collection?
3What is mutability and why is it dangerous in certain scenarios?
4What is cloning?
5Differentiate between deep and shallow copies
6How nested lists are stored in memory?
7How strings are stored in memory
8Why tuples take less memory than lists?
9How set index position is decided?
10Why 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 = a after a = 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 a is assigned a string value 'AI 2026-27', creating a reference to that string in memory.
  • The variable b is then assigned the value of a, which means b now references the same string object as a.
  • Similarly, c is assigned the value of b, making c another 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 sys module is imported to access system-specific parameters and functions.
  • The getrefcount function 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 gc module, which provides access to the garbage collection facilities in Python.
  • It calls the get_threshold() function from the gc module, 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 print statement 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.

Browse Free Posts

Already a premium member? Sign in here

Python Interview Questions & Answers Guide | Madhu Dadi