Online Test

1). What will be the output of the following code??

def func(a, b=[]):
    b.append(a)
    return b

print(func(1))
print(func(2))

2). Which of the following methods is used to create a shallow copy of a list in Python??

x = [1, 2, 3]
y = x.copy()

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))

7). How can you handle the `KeyboardInterrupt` exception in Python??

try:
    while True:
        pass
except KeyboardInterrupt:
    print("Interrupted!")

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}

12). How can you generate random numbers in Python??

import random

print(random.randint(1, 10))

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)

15). What is the result of the following code??

def f(x=[]):
    x.append(1)
    return x

print(f())
print(f())

16). How do you check if an object is an instance of a particular class in Python??

class MyClass:
    pass

obj = MyClass()
print(isinstance(obj, MyClass))

17). What is the purpose of the `with` statement in Python??

with open('file.txt', 'w') as f:
    f.write('Hello, World!')

18). What is the output of the following generator expression??

gen = (x ** 2 for x in range(3))
for i in gen:
    print(i)

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))

Test Results