Python List MCQ : Set 2

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.

11). What is the output of the following code?
a=[23,56,12,89]
a.sort()
print(“Smallest number: “, a[0])
 
a) Smallest number: 12
b) Smallest number: 23
c) Smallest number: 56
d) Smallest number: 89
 
Correct answer is: a) Smallest number: 12
Explanation: The list ‘a’ is sorted in ascending order using the sort() method, and then the smallest number, which is the first element of the sorted list, is printed.
 
12). What is the value of even list after executing the code snippet?
 
og_list=[23,11,78,90,34,55]
odd=[]
even=[]
for i in og_list:
    if i%2==0:
        even.append(i)
    else:
        odd.append(i)
print(“Even numbers: “,even)
print(“Odd numbers: “,odd)
 
a) [23, 11, 55]
b) [78, 90, 34]
c) [23, 11, 78, 90, 34, 55]
d) []
 
Correct answer is: b) [78, 90, 34]
Explanation: The even list stores the even numbers from og_list, which are 78, 90, and 34.
 
13). What is the purpose of the odd list in the code snippet?
 
og_list=[23,11,78,90,34,55]
odd=[]
even=[]
for i in og_list:
    if i%2==0:
        even.append(i)
    else:
        odd.append(i)
print(“Even numbers: “,even)
print(“Odd numbers: “,odd)
 
a) To store odd numbers from og_list
b) To store even numbers from og_list
c) To store all numbers from og_list
d) It has no purpose in the code snippet
 
Correct answer is: a) To store odd numbers from og_list
Explanation: The code snippet checks each number in og_list and appends the odd numbers to the odd list.
 
14). How does the code remove duplicate elements from list ‘a’?
 
a=[5,7,8,2,5,0,7,2]
b=[]
for i in a:
    if i not in b:
        b.append(i)
print(b)
 
a) It uses the remove() method to delete duplicate elements
b) It utilizes the pop() method to remove duplicate elements
c) It compares each element of ‘a’ with elements in ‘b’ and appends if not present
d) It employs the filter() function to filter out duplicate elements
 
Correct answer is: c) It compares each element of ‘a’ with elements in ‘b’ and appends if not present
Explanation: The code iterates through each element of ‘a’ and checks if it is already present in ‘b’. If not, it appends the element to ‘b’.
 
15). What is the output of the following code?
 
a=[5,7,8,2,5,0,7,2]
b=[]
for i in a:
    if i not in b:
        b.append(i)
print(b)
 
a) [5, 7, 8, 2, 5, 0, 7, 2]
b) [5, 7, 8, 2, 0]
c) [5, 7, 8, 2, 5, 0]
d) [5, 7, 8, 2, 0, 7]
 
Correct answer is: b) [5, 7, 8, 2, 0]
Explanation: The code removes duplicate elements from the list ‘a’ and stores the unique elements in list ‘b’.
 
16). What is the purpose of the condition ‘if i%2==0’ in the code?
 
a=[2,4,7,8,5,1,6]
for i in a:
    if i%2==0:
        print(i,”:”,i**2)
 
a) To check if ‘i’ is an even number
b) To check if ‘i’ is an odd number
c) To check if ‘i’ is divisible by 3
d) To check if ‘i’ is divisible by 5
 
Correct answer is: a) To check if ‘i’ is an even number
Explanation: The condition ‘if i%2==0’ checks if the element ‘i’ is divisible by 2, indicating it is an even number.
 
17). What is the value of `common_list` after executing the given code snippet?
 
list1 = [4, 5, 7, 9, 2, 1]
list2 = [2, 5, 8, 3, 4, 7]
common_list = []
for i in list1:
    if i in list2:
        common_list.append(i)
common_list
 
a) [2, 5, 7]
b) [4, 5, 7]
c) [2, 5, 8, 3, 4, 7]
d) []
 
Correct answer is: a) [2, 5, 7]
Explanation: The code iterates through each element in `list1`. If the element exists in `list2`, it appends it to the `common_list`. Therefore, the common elements between `list1` and `list2` are [2, 5, 7], which will be the resulting value of `common_list`.
 
18). What does the range(len(a)-1, -1, -1) expression represent in the code?
 
a=[1,2,3,4,55]
for i in range(len(a)-1,-1,-1):
    print(a[i],end=” “)
 
a) It generates a sequence of numbers from the length of list a to -1 in descending order.
b) It generates a sequence of numbers from the length of list a to 0 in descending order.
c) It generates a sequence of numbers from -1 to the length of list a in descending order.
d) It generates a sequence of numbers from -1 to 0 in descending order.
 
Correct answer is: b) It generates a sequence of numbers from the length of list a to 0 in descending order.
Explanation: The range() function is used to generate a sequence of indices starting from len(a)-1 (the last index of the list) down to 0 in reverse order.
 
19). What is the purpose of the end=” ” parameter in the print() function?
 
a) It adds a space character after each printed element.
b) It separates the printed elements by a space character.
c) It specifies the ending character for each line of output.
d) It removes any whitespace between the printed elements.
 
Correct answer is: a) It adds a space character after each printed element.
Explanation: The end=” ” parameter in the print() function ensures that a space character is added after each element is printed, resulting in the elements being separated by spaces in the output.
 
20). What does the reverse() method do in the Python list?
 
a) Reverses the order of elements in the list ‘x’
b) Sorts the elements of the list ‘x’ in ascending order
c) Removes duplicate elements from the list ‘x’
d) Inserts a new element at the end of the list ‘x’
 
Correct answer is: a) Reverses the order of elements in the list ‘x’
Explanation: The reverse() method is used to reverse the order of elements in a 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].

Leave a Comment