Online Test

1). What is the output of the following code??

x = {1: 'a', 2: 'b'}
print(x[1])

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

x = [1, 2, 3]
x.append([4, 5])
print(len(x))

3). What is the output of the following code??

x = [1, 2, 3]
print(x[2:])

4). How do you create a dictionary in Python??

my_dict = {'key1': 'value1', 'key2': 'value2'}

5). What is the output of the following code??

x = (1, 2, 3)
print(x[1])

6). What does `len()` function do in Python??

len('string')

7). What is the output of the following code??

x = [1, 2, 3]
x.insert(1, 4)
print(x)

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

x = {'a': 1, 'b': 2}
y = x.get('a')
print(y)

9). What does the `pop()` method do in Python??

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

10). How do you define a function in Python??

def my_function():
    pass

11). How can you handle exceptions in Python??

try:
    # code
except Exception as e:
    pass

12). What is the output of the following code??

x = {'a': 1, 'b': 2}
y = x.keys()
print(list(y))

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

x = [1, 2, 3]
print(x[::-1])

14). How do you create a tuple in Python??

my_tuple = (1, 2, 3)

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

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

16). What does the `extend()` method do in Python??

x = [1, 2]
y = [3, 4]
x.extend(y)
print(x)

17). What is the output of the following code??

x = {'a': 1, 'b': 2}
y = x.popitem()
print(y)

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

x = '   python   '
print(x.strip())

19). How do you sort a list in place in Python??

x = [3, 1, 2]
x.sort()
print(x)

20). What is the output of the following code??

x = [1, 2, 3]
y = [4, 5]
print(x + y)

21). What does `enumerate()` do in Python??

for index, value in enumerate(['a', 'b', 'c']):
    print(index, value)

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

x = {'a': 1, 'b': 2}
y = x.setdefault('c', 3)
print(y)

23). How do you concatenate two lists in Python??

x = [1, 2]
y = [3, 4]
print(x + y)

24). What is the output of the following code??

x = 'hello'
print(x[1:4])

25). What does `list()` function do in Python??

list('abc')

26). How do you remove duplicates from a list in Python??

x = [1, 2, 2, 3]
print(list(set(x)))

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

x = [1, 2, 3]
print(x.index(2))

28). How can you create a new file in Python??

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

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

x = [1, 2, 3]
print(x.pop(0))

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

x = [1, 2, 3]
print(x[::-2])

31). How do you check if a key exists in a dictionary??

x = {'a': 1}
'a' in x

32). What is the output of the following code??

x = [1, 2, 3]
print(x[1:2])

33). What does the `count()` method do in Python??

x = [1, 2, 2, 3]
print(x.count(2))

34). How do you remove an item from a list by index??

x = [1, 2, 3]
print(x.pop(1))

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

x = [1, 2, 3]
print(x[::2])

36). How do you combine two dictionaries in Python 3.9+??

dict1 = {'a': 1}
dict2 = {'b': 2}
merged = dict1 | dict2
print(merged)

37). What is the output of the following code??

x = (1, 2, 3)
print(len(x))

38). How do you check the type of a variable in Python??

x = 5
print(type(x))

39). What is the output of the following code??

x = 'abc'.replace('a', 'A')
print(x)

40). How do you create a class in Python??

class MyClass:
    def __init__(self):
        pass

41). What is the output of the following code??

x = [1, 2, 3]
print(x.count(1))

42). How do you iterate over a list in Python??

for item in [1, 2, 3]:
    print(item)

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

x = {'a': 1, 'b': 2}
print(x.pop('a'))

44). What is the output of the following code??

x = [1, 2, 3]
print(x[1:])

45). What does the `insert()` method do in Python??

x = [1, 2]
x.insert(1, 3)
print(x)

46). What is the output of the following code??

x = [1, 2, 3]
print(x[::-1])

47). How do you create a new dictionary in Python??

x = {'a': 1, 'b': 2}

48). What is the output of the following code??

x = [1, 2, 3]
print(x[-1])

49). How do you remove a key from a dictionary in Python??

x = {'a': 1, 'b': 2}
print(x.pop('a'))

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

x = [1, 2, 3]
print(x[1])

51). What does `split()` method do in Python??

x = 'hello world'.split()
print(x)

52). What is the output of the following code??

x = [1, 2, 3]
print(x[::3])

Test Results