3). What is the output of the following code??
a = [1, 2, 3]
b = [4, 5]
print([a[i] + b[i] for i in range(min(len(a), len(b)))])
4). Which of the following is true about the GIL (Global Interpreter Lock) in Python??
import threading
def worker():
print("Working...")
for _ in range(3):
thread = threading.Thread(target=worker)
thread.start()
5). What will be the output of the following code??
def f(a, b, *args, **kwargs):
print(a, b, args, kwargs)
f(1, 2, 3, 4, x=5, y=6)
6). What is the result of the following code??
from collections import Counter
c = Counter(['a', 'b', 'c', 'a', 'b', 'b'])
print(c.most_common(1))
8). What is the output of the following code??
def f(x, y):
return x + y
result = (lambda x, y: x * y)(2, 3)
print(result)
9). What is the main use of Python's `os.path` module??
import os
print(os.path.join('folder', 'file.txt'))
10). What is the difference between `__str__()` and `__repr__()` methods in Python??
class Example:
def __str__(self):
return "str method"
def __repr__(self):
return "repr method"
print(str(Example()))
print(repr(Example()))
11). Which of the following is an immutable data type in Python??
x = (1, 2, 3)
y = [1, 2, 3]
z = {'a': 1, 'b': 2}
13). What is the purpose of Python's `@staticmethod` decorator??
class MyClass:
@staticmethod
def my_static_method():
return "Hello, World!"
print(MyClass.my_static_method())
14). What is the output of the following code when using list comprehension??
matrix = [[1, 2], [3, 4], [5, 6]]
flat = [num for row in matrix for num in row]
print(flat)
17). What is the purpose of the `with` statement in Python??
with open('file.txt', 'w') as f:
f.write('Hello, World!')
19). How can you prevent a function from being overridden in a subclass??
class MyClass:
def final_method(self):
pass
# Attempt to override
class MySubClass(MyClass):
def final_method(self):
pass
20). What is the purpose of the `assert` statement in Python??
def test(x):
assert x > 0, "Value must be positive!"
print(test(-1))