25 Must Know Python IQ
25 Must Know Python IQ
Know
Python
INTERVIEW QUESTIONS
NOTE
These are advance level question that are actually
acted these days, and you can’t answer these if you
don’t learn these before.
Curated
Curatedby
by
Q.1
What is the Global Interpreter Lock (GIL) in Python,
and how does it impact multi-threading?
While the GIL makes Python threads less efficient for CPU-bound
tasks, it is not a problem for I/O-bound tasks, as threads can yield
the GIL during I/O operations. To achieve parallelism in CPU-
bound tasks, you can use the multiprocessing module, which
creates separate processes, each with its own Python interpreter
and memory space.
import threading
def count_up():
for _ in range(1000000):
pass
def count_down():
for _ in range(1000000):
pass
thread1 = threading.Thread(target=count_up)
thread2 = threading.Thread(target=count_down)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
Curated
Curatedby
by
Q.2
duplicate objects:
Shallow Copy: A shallow copy creates a new object but does not
import copy
shallow_copied_list = copy.copy(original_list)
of the original object and all its nested objects. You can create a
import copy
deep_copied_list = copy.deepcopy(original_list)
Curated
Curatedby
by
Q.3
changing their source code. They are often used for tasks such as
time of a function:
import time
def timing_decorator(func):
start_time = time.time()
end_time = time.time()
seconds to execute.")
return result
return wrapper
@timing_decorator
def some_function():
time.sleep(2)
some_function()
Curated
Curatedby
by
Q.4
a regular function?
Fibonacci numbers:
def fibonacci_generator():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
# Usage:
fib_gen = fibonacci_generator()
for _ in range(10):
print(next(fib_gen))
using the return statement, and they do not retain their state
between calls.
Curated
Curatedby
by
Q.5
Python classes:
class MyClass:
self.x = x
the object.
class MyClass:
def __del__(self):
obj = MyClass()
often handles object destruction automatically, and you may not need to
Curated
Curatedby
by
Q.6
Example of a closure:
def outer_function(x):
def inner_function(y):
return x + y
return inner_function
closure = outer_function(10)
From To
Success
Ashish Joshi
Story
Curated
Curatedby
by
Q.7
provide an example.
subclass.
class Parent:
self.x = x
class Child(Parent):
super().__init__(x)
self.y = y
print(child_obj.x) # Output: 10
print(child_obj.y) # Output: 20
Curated
Curatedby
by
Q.8
how is it determined?
Code- class A:
def method(self):
print("A method")
class B(A):
def method(self):
print("B method")
class C(A):
def method(self):
print("C method")
pass
d = D()
Curated
Curatedby
by
Q.9
programming?
The async and await keywords are used to define and work with
import asyncio
asyncio.to_thread(requests.get, url),
asyncio.to_thread(requests.get, url)
return response
# Usage:
result = asyncio.run(fetch_data("https://tutort.com"))
Curated
Curatedby
by
Q.10
import multiprocessing
def worker_function(x):
return x * x
if __name__ == "__main__":
pool = multiprocessing.Pool(processes=4)
pool.close()
pool.join()
Curated
Curatedby
by
Q.11
a mutex (short for mutual exclusion) that allows only one thread
While the GIL makes Python threads less efficient for CPU-bound
import threading
def count_up():
for _ in range(1000000):
pass
def count_down():
for _ in range(1000000):
pass
thread1 = threading.Thread(target=count_up)
thread2 = threading.Thread(target=count_down)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
Curated
Curatedby
by
Q.12
Shallow Copy: A shallow copy creates a new object but does not
import copy
shallow_copied_list = copy.copy(original_list)
of the original object and all its nested objects. You can create a
import copy
deep_copied_list = copy.deepcopy(original_list)
Curated
Curatedby
by
Q.13
memoization.
time of a function:
import time
def timing_decorator(func):
start_time = time.time()
end_time = time.time()
seconds to execute.")
return result
return wrapper
@timing_decorator
def some_function():
time.sleep(2)
some_function()
Curated
Curatedby
by
Q.14
a regular function?
Fibonacci numbers:
def fibonacci_generator():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
# Usage:
fib_gen = fibonacci_generator()
for _ in range(10):
print(next(fib_gen))
Curated
Curatedby
by
Q.15
class MathUtility:
@staticmethod
return x + y
result = MathUtility.add(5, 3)
class itself as its first argument. Class methods are often used for
@classmethod
class MyClass:
def get_instance_count(cls):
count = 0
return cls.count
obj1 = MyClass(10)
self.value = value
obj2 = MyClass(20)
MyClass.count += 1
count = MyClass.get_instance_count() # count is 2
Curated
Curatedby
by
Q.16
Explain the purpose of the @property decorator in
Python, and provide an example of its usage.
class Circle:
self._radius = radius
@property
def radius(self):
return self._radius
@radius.setter
if value < 0:
raise ValueError("Radius
cannot be negative")
self._radius = value
@property
def area(self):
circle = Circle(5)
Curated
Curatedby
by
Q.17
class MyClass:
def __str__(self):
the object. It is called by the repr() function and can also be used
for debugging.
class MyClass:
self.x = x
def __repr__(self):
return f"MyClass({self.x})"
Curated
Curatedby
by
Q.18
manager.
resources and ensure that they are properly managed, even in the
statement.
class MyContextManager:
def __enter__(self):
# Usage:
Curated
Curatedby
by
Q.19
class MyClass:
def __str__(self):
It is called by the repr() function and can also be used for debugging.
class MyClass:
self.x = x
def __repr__(self):
return f"MyClass({self.x})"
Curated
Curatedby
by
Q.20
manager.
resources and ensure that they are properly managed, even in the
statement.
class MyContextManager:
def __enter__(self):
# Usage:
Curated
Curatedby
by
Q.21
From To
Success
Gen Story
Ajay Kumar
Curated
Curatedby
by
Q.22
it be used?
# Original class
class Calculator:
return x + y
return x * y
Calculator.multiply = multiply
# Usage
calc = Calculator()
Curated
Curatedby
by
Q.23
class MyMeta(type):
pass
class MyClass(metaclass=MyMeta):
self.x = x
Guaranteed
Hiring
Highest
Curated
Curatedby
by
Q.24
import multiprocessing
def worker_function(x):
return x * x
if __name__ == "__main__":
pool = multiprocessing.Pool(processes=4)
pool.close()
pool.join()
Curated
Curatedby
by
Q.25
systems.
variable_name = "x"
variable_value = 42
exec(dynamic_code)
print(x) # Output: 42
Curated
Curatedby
by
Start Your
Upskilling with us
Explore our courses
www.tutort.net
Follow us on