Python List MCQ
1). Which method is used to check if any element in a list satisfies a certain condition?
a) any()
b) some()
c) exists()
d) satisfy_any()
Correct answer is: a) any()
Explanation: The any() method is used to check if any element in a list satisfies a certain condition.
2). Which method is used to sort a list based on a custom key function?
a) sort()
b) arrange()
c) organize()
d) custom_sort()
Correct answer is: a) sort()
Explanation: The sort() method is used to sort a list based on a custom key function.
3). Which method is used to reverse the order of elements in a list?
a) reverse()
b) flip()
c) invert()
d) back()
Correct answer is: a) reverse()
Explanation: The reverse() method is used to reverse the order of elements in a list.
4). Which method is used to make a shallow copy of a list?
a) copy()
b) duplicate()
c) clone()
d) replicate()
Correct answer is: a) copy()
Explanation: The copy() method is used to make a shallow copy of a list.
5). What is the output of the following code?
l = [2, 5, 7, 8]
for i in l:
print(i ** 2)
a) 4, 25, 49, 64
b) 4, 10, 14, 16
c) 2, 5, 7, 8
d) 2, 25, 49, 64
Correct answer is: a) 4, 25, 49, 64
Explanation: The code iterates through each element in the list “l” and prints the square of each element.
6). What is the output of the following code?
a = [3, 5, 7, 1, 8]
count = 0
while count < len(a):
print(a[count], “:”, a[count]**2)
count += 1
a) 3: 3, 5: 5, 7: 7, 1: 1, 8: 8
b) 9, 25, 49, 1, 64
c) 3: 9, 5: 25, 7: 49, 1: 1, 8: 64
d) 3, 5, 7, 1, 8
Correct answer is: c) 3: 9, 5: 25, 7: 49, 1: 1, 8: 64
Explanation: The code iterates over the list ‘a’ and prints each element followed by its square.
7). What is the output of the following code?
a = [2, 5, 7, 9]
b = [6, 3, 0]
for i in a:
b.append(i)
print(b)
a) [6, 3, 0, 2, 5, 7, 9]
b) [2, 5, 7, 9, 6, 3, 0]
c) [2, 5, 7, 9]
d) [6, 3, 0, 9, 7, 5, 2]
Correct answer is: a) [6, 3, 0, 2, 5, 7, 9]
Explanation: The code iterates over each element in list `a` and appends it to the end of list `b`. The resulting list `b` contains the elements from `b` followed by the elements from `a`.
8). What is the output of the following code?
list1 = [2, 5, 8, 0, 1]
count = 0
total = 0
while count < len(list1):
total += list1[count]
count += 1
print(total)
a) 0
b) 2
c) 16
d) 16, Error: list index out of range
Correct answer is: c) 16
Explanation: The code snippet initializes the variables `count` and `total` to 0. It then enters a while loop that iterates as long as `count` is less than the length of `list1`. Within the loop, it adds the value at index `count` in `list1` to the `total` variable and increments `count` by 1. After the loop, it prints the final value of `total`, which is the sum of all elements in `list1`. In this case, the sum is 2 + 5 + 8 + 0 + 1, which equals 16.
9). What is the output of the following code?
list1 = [3, 6, 9, 2]
product = 1
count = 0
while count < len(list1):
product *= list1[count]
count += 1
print(product)
a) 0
b) 1
c) 54
d) 324
Correct answer is: d) 324
Explanation: The code calculates the product of all the elements in the list1. The initial value of product is 1, and it gets multiplied with each element in the list (3 * 6 * 9 * 2), resulting in 324. The value of count starts at 0 and increments until it reaches the length of list1.
10). What happens to the original list ‘a’ after calling the sort() method?
a) The list ‘a’ is sorted in descending order.
b) The list ‘a’ remains unchanged.
c) The list ‘a’ is sorted in ascending order.
d) An error occurs because the sort() method cannot be used on the list ‘a’.
Correct answer is: c) The list ‘a’ is sorted in ascending order.
Explanation: The sort() method sorts the elements of the list ‘a’ in ascending order, modifying the original list.
21). Which of the following methods could be used to achieve the same result as reverse() in the Python list?
a) sort()
b) pop()
c) append()
d) None of the above
Correct answer is: a) sort()
Explanation: The sort() method could be used to sort the elements of the list in reverse order, achieving the same result as reverse(). However, it is not the most efficient way to reverse a list.
22). What is the output of the following code?
x = [2, 3, 7, 9, 3, 1]
print(“Original list: “, x)
x.reverse()
print(“Using reverse: “, x)
a) Original list: [2, 3, 7, 9, 3, 1]
Using reverse: [1, 3, 9, 7, 3, 2]
b) Original list: [2, 3, 7, 9, 3, 1]
Using reverse: [2, 3, 7, 9, 3, 1]
c) Original list: [2, 3, 7, 9, 3, 1]
Using reverse: [1, 2, 3, 3, 7, 9]
d) Original list: [2, 3, 7, 9, 3, 1]
Using reverse: [9, 7, 3, 1, 3, 2]
Correct answer is: a) Original list: [2, 3, 7, 9, 3, 1]
Using reverse: [1, 3, 9, 7, 3, 2]
Explanation: The code reverses the order of elements in the list ‘x’ using the reverse() method.
23). What is the output of the following code?
a = [3, 5, -8, 0, -20, -55]
for i in a:
if i >= 0:
print(i, end=” “)
a) 3 5 0
b) 3 5 -8 0 -20 -55
c) 3 5
d) 3 5 -8
Correct answer is: a) 3 5 0
Explanation: The code iterates over each element in the list `a`. If the element is greater than or equal to 0, it is printed with a space as the end character. Therefore, only the positive elements (3, 5, and 0) are printed.
24). 1. What is the output of the following code?
a=[2,4,6,6,4,2]
b=a[::-1]
if a==b:
print(“List is palindrome”)
else:
print(“List is not palindrome”)
a) “List is palindrome”
b) “List is not palindrome”
c) Error: ‘==’ not supported between lists
d) Error: ‘print’ is not a valid function
Correct answer is: a) “List is palindrome”
Explanation: The given code checks if the list ‘a’ is a palindrome by reversing it and comparing it with the original list. Since they are the same, the output will be “List is palindrome”.
25). What is the value of ‘a’ after executing the following code?
a = [33, 56, 89, 12, 45]
a.pop(2)
a) [33, 56, 89, 12, 45]
b) [33, 56, 12, 45]
c) [33, 56, 12]
d) [33, 89, 12, 45]
Correct answer is: b) [33, 56, 12, 45]
Explanation: The `pop(2)` method removes and returns the element at index 2 from list ‘a’. In this case, the element 89 is removed from the list, resulting in the updated list [33, 56, 12, 45].