Python List MCQ : Set 3

Python List MCQ

1). What is the output of the following code?
 
a = [22, 66, 89, 9, 44]
print(“Minimum: “, min(a))
 
a) Minimum: 9
b) Minimum: 22
c) Minimum: 44
d) Minimum: 89
 
Correct answer is: a) Minimum: 9
Explanation: The min() function returns the minimum value from the list ‘a’, which is 9 in this case.
 
2). What is the purpose of the following code?
 
a = [22, 45, 67, 12, 78]
b = [45, 12]
count = 0
for i in a:
    for j in b:
        if i == j:
            count += 1
if count == len(b):
    print(“Sublist exists”)
else:
    print(“Sublist does not exist”)
 
a) To find the common elements between lists a and b
b) To check if list b is a subset of list a
c) To concatenate lists a and b
d) To count the number of occurrences of elements in list b within list a.
 
Correct answer is: b) To check if list b is a subset of list a
Explanation: The code checks if every element in list b exists in list a. If all elements of b are found in a, the count variable will be equal to the length of b, indicating that b is a sublist or subset of a. Otherwise, it means that b is not a sublist or subset of a.
 
3). What is the output of the following code?
 
a = [“a”, “b”, “c”, “d”]
print(“$”.join(a))
 
a) “a$b$c$d”
b) “abcd”
c) [“a”, “b”, “c”, “d”]
d) Error: cannot join a list with a string delimiter
 
Correct answer is: a) “a$b$c$d”
Explanation: The `join()` method is used to concatenate the elements of a list into a single string, with the specified delimiter inserted between each element. In this case, the elements of list `a` are joined together using the delimiter “$”, resulting in the string “a$b$c$d”.
 
4). 1. What is the output of the following code?
 
a = [“Sqa”, “Tools”, “Online”, “Learning”, “Platform’”]
b = []
for i in a:
    b.append(i[::-1])
print(b)
 
a) [“aqS”, “slooT”, “enilnO”, “gninraeL”, “mocalfP”]
b) [“asqS”, “slooT”, “enilnO”, “gninraeL”, “mocalfP”]
c) [“Sqa”, “Tools”, “Online”, “Learning”, “Platform”]
d) [“aS”, “looT”, “enilgnO”, “gninraeL”, “mocalfP”]
 
Correct answer is: a) [“aqS”, “slooT”, “enilnO”, “gninraeL”, “mocalfP”]
Explanation: In the code, a loop iterates through each element of list ‘a’. The `i[::-1]` expression reverses each string in ‘a’. The reversed strings are then appended to the empty list ‘b’. Finally, ‘b’ is printed, which contains the reversed strings from ‘a’.
 
5). What is the purpose of the zip() function in the Python list?
 
a) Concatenate two lists into one.
b) Multiply corresponding elements of two lists.
c) Iterate over elements from two lists simultaneously.
d) Find the common elements between two lists.
 
Correct answer is: c) Iterate over elements from two lists simultaneously.
Explanation: The zip() function is used to combine elements from multiple lists into tuples. In the given code, it allows iterating over elements from list1 and list2 at the same time.
 
6). What is the output of the following code?
 
list1 = [3, 5, 7, 8, 9]
list2 = [1, 4, 3, 6, 2]
final=[]
for (a,b) in zip(list1,list2):
    final.append(list((a,b)))
print(final)
 
a) [[3, 1], [5, 4], [7, 3], [8, 6], [9, 2]]
b) [[1, 3], [4, 5], [3, 7], [6, 8], [2, 9]]
c) [[1, 4, 3, 6, 2]]
d) Error: ‘tuple’ object has no attribute ‘append’
 
Correct answer is: a) [[3, 1], [5, 4], [7, 3], [8, 6], [9, 2]]
Explanation: The code uses the zip() function to iterate over elements from list1 and list2 simultaneously. It creates a list of tuples where each tuple contains corresponding elements from both lists.
 
7). What is the output of the following code?
 
list1 = [{“a”:12}, {“b”: 34}, {“c”: 23}, {“d”: 11}, {“e”: 15}]
key = []
value = []
for element in list1:
    for val in element:
        key.append(val)
        value.append(element[val])
print(“Key : “, key)
print(“Value : “, value)
 
a) Key : [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]  Value : [12, 34, 23, 11, 15]
b) Key : [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]  Value : [{‘a’:12}, {‘b’: 34}, {‘c’: 23}, {‘d’: 11}, {‘e’: 15}]
c) Key : [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]  Value : [12, 34, 23, 11, 15, ‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
d) Key : [{‘a’:12}, {‘b’: 34}, {‘c’: 23}, {‘d’: 11}, {‘e’: 15}]  Value : [12, 34, 23, 11, 15]
  
Correct answer is: a) Key : [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]  Value : [12, 34, 23, 11, 15]
Explanation: The code iterates over the list1, extracting the keys and values from each dictionary. The keys are appended to the ‘key’ list, and the values are appended to the ‘value’ list. Finally, the code prints the contents of the ‘key’ and ‘value’ lists.
 
8). Which method can be used to split a string into a list based on a different delimiter?
 
a) split()
b) separate()
c) divide()
d) break()
 
Correct answer is: a) split()
Explanation: The split() method is used to split a string into a list based on a specified delimiter.
 
9). What is the correct way to create an empty list in Python?
 
a) list = []
b) list = ()
c) list = {}
d) list = None
 
Correct answer is: a) list = []
Explanation: To create an empty list in Python, we use square brackets [] without any elements inside.
 
10). What is the output of the following code snippet?
 
my_list = [1, 2, 3]
my_list.append(4)
print(len(my_list))
 
a) 1
b) 2
c) 3
d) 4
 
Correct answer is: d) 4
Explanation: The append() method is used to add an element to the end of a list. In this case, it adds the element 4 to my_list, increasing its length to 4.

11). Which method is used to remove an element from a list by its value?

a) remove()
b) delete()
c) pop()
d) discard()

Correct answer is: a) remove()
Explanation: The remove() method is used to remove the first occurrence of a specified element from a list.

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

my_list = [1, 2, 3, 4, 5]
sliced_list = my_list[1:5]
print(sliced_list)

a) [2, 3, 4, 5]
b) [1, 2, 3]
c) [1, 2, 3, 4]
d) [4, 5]

Correct answer is: a) [2, 3, 4, 5]
Explanation: The slicing operation my_list[1:4] creates a new list containing elements from index 1 to index 4. Hence, the output is [2, 3, 4, 5]

13). Which method is used to find the index of the last occurrence of an element in a list?

a) index()
b) rindex()
c) find()
d) search()

Correct answer is: b) rindex()
Explanation: The rindex() method is used to find the index of the last occurrence of an element in a list. It starts searching from the end of the list and returns the index of the last match.

14). Which function is used to find the minimum value from a list?

a) min()
b) minimum()
c) smallest()
d) find_min()

Correct answer is: a) min()
Explanation: The min() function is used to find the minimum value from a list.

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

list1 = [12, 32, 33, 5, 4, 7]
list1[0] = “SQA”
list1[-1] = “TOOLS”
print(list1)

a) [12, 32, 33, 5, 4, 7]
b) [“SQA”, 32, 33, 5, 4, “TOOLS”]
c) [“SQA”, 32, 33, 5, 4, 7]
d) [12, 32, 33, 5, 4, “TOOLS”]

Correct answer is: b) [“SQA”, 32, 33, 5, 4, “TOOLS”]
Explanation: In the given code, the element at index 0 of `list1` is replaced with the string “SQA” using the assignment operator (`=`). Similarly, the element at index -1 (last index) of `list1` is replaced with the string “TOOLS”. Therefore, the resulting list is `[“SQA”, 32, 33, 5, 4, “TOOLS”]`.

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

list1 = [22, 45, 67, 11, 90, 67]
print(“Is 67 exist in list1? “, 67 in list1)

a) Is 67 exist in list1? True
b) Is 67 exist in list1? False
c) Is 67 exist in list1? Error
d) True

Correct answer is: a) Is 67 exist in list1? True
Explanation: The `in` keyword is used to check if an element exists in a list. In this case, the code checks if 67 exists in `list1`, and since it does, the output will be “Is 67 exist in list1? True”.

17). What is the output of the following code?
list1 = [1, 2, 3, 4, 5]
print([‘Sqa{0}’.format(value) for value in list1])

a) [‘Sqa1’, ‘Sqa2’, ‘Sqa3’, ‘Sqa4’, ‘Sqa5’]
b) [‘Sqa[1]’, ‘Sqa[2]’, ‘Sqa[3]’, ‘Sqa[4]’, ‘Sqa[5]’]
c) [‘Sqa{0}’.format(1), ‘Sqa{0}’.format(2), ‘Sqa{0}’.format(3), ‘Sqa{0}’.format(4), ‘Sqa{0}’.format(5)]
d) [‘Sqa0’, ‘Sqa1’, ‘Sqa2’, ‘Sqa3’, ‘Sqa4’]

Correct answer is: a) [‘Sqa1’, ‘Sqa2’, ‘Sqa3’, ‘Sqa4’, ‘Sqa5’]
Explanation: The code uses a list comprehension to iterate over each value in list1 and format it with ‘Sqa{0}’ to create a new list with formatted strings.

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

list1 = [[11, 2, 3], [4, 15, 2], [10, 11, 12], [7, 8, 19]]
print(max(list1, key=sum))

a) [11, 2, 3]
b) [4, 15, 2]
c) [10, 11, 12]
d) [7, 8, 19]

Correct answer is: d) [7, 8, 19]
Explanation: The `max()` function is used to find the maximum element in a list. In this case, `key=sum` is passed as an argument to specify that the sum of each sub-list should be used as the criterion for comparison. Since the sum of `[7, 8, 19]` is the highest among all the sub-lists, it will be returned as the output.

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

list1 = [2, 4, 6, 8, 3, 22]
list1.insert(3, 55)
print(list1)

a) [2, 4, 6, 8, 3, 22, 55]
b) [2, 4, 55, 6, 8, 3, 22]
c) [2, 4, 6, 55, 8, 3, 22]
d) [2, 4, 6, 8, 55, 3, 22]

Correct answer is: c) [2, 4, 6, 55, 8, 3, 22]
Explanation: The `insert()` method is used to insert an element at a specific position in the list. In this case, element 55 is inserted at index 3, shifting the remaining elements to the right. The resulting list becomes [2, 4, 6, 55, 8, 3, 22].

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

list1 = [“Learn”, “python”, “From”, “Sqa”, “tools”]
list2 = []
for words in list1:
    list2.append(words[0].upper() + words[1:-1] + words[-1].upper() + ” “)
print(“Uppercase: “, list2)

a) Uppercase: [“LearN”, “pythoN”, “FroM”, “SqA”, “ToolS”]
b) Uppercase: [“Learn”, “python”, “From”, “Sqa”, “tools”]
c) Uppercase: [“L”, “p”, “F”, “S”, “t”]
d) Uppercase: [“LEARN”, “PYTHON”, “FROM”, “SQA”, “TOOLS”]

Correct answer is: a) Uppercase: [“LearN”, “pythoN”, “FroM”, “SqA”, “ToolS”]
Explanation: The code capitalizes the first and last letters of each word in list1. The resulting list, list2, contains the modified words.

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

list1 = [[3, 5, 6], [2, 1, 3], [5, 1, 1], [1, 2, 1], [0, 4, 1]]
print(sorted(list1, key=sum))

a) [[0, 4, 1], [1, 2, 1], [2, 1, 3], [3, 5, 6], [5, 1, 1]]
b) [[5, 1, 1], [1, 2, 1], [2, 1, 3], [3, 5, 6], [0, 4, 1]]
c) [[3, 5, 6], [2, 1, 3], [5, 1, 1], [1, 2, 1], [0, 4, 1]]
d) [[1, 2, 1], [2, 1, 3], [0, 4, 1], [3, 5, 6], [5, 1, 1]]

Correct answer is: d) [[1, 2, 1], [2, 1, 3], [0, 4, 1], [3, 5, 6], [5, 1, 1]]
Explanation: The `sorted()` function is used to sort the list `list1` based on a custom key function, which is the `sum()` function in this case. The `sum()` function calculates the sum of each sublist in `list1`. Sorting the list based on the sum will result in ascending order based on the sum of each sublist’s elements.

22). If the list ‘a’ is empty, what will be the output of the given code?

a = []
print(“Minimum: “, min(a))

a) Minimum: None
b) Minimum: 0
c) Error: min() arg is an empty sequence
d) Error: ‘list’ object has no attribute ‘min’

Correct answer is: c) Error: min() arg is an empty sequence
Explanation: The min() function raises an error if it is called on an empty list.

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

list1 = [“Python”, “Sqatools”, “Practice”, “Program”, “test”, “lists”]
list2 = []

for string in list1:
    if len(string) > 7:
        list2.append(string)
print(list2)

a) [“Python”, “Sqatools”, “Practice”, “Program”]
b) [“Python”, “Sqatools”, “Practice”, “Program”, “lists”]
c) [“Python”, “Sqatools”, “Practice”, “Program”, “test”]
d) []

Correct answer is: b) [“Python”, “Sqatools”, “Practice”, “Program”, “lists”]
Explanation: The code iterates over each string in `list1`. If the length of the string is greater than 7, it is appended to `list2`. In this case, “Python”, “Sqatools”, “Practice”, “Program”, and “lists” have lengths greater than 7, so they are added to `list2`. Therefore, the output is `[“Python”, “Sqatools”, “Practice”, “Program”, “lists”]`.

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

list1 = [1, 1, 3, 4, 4, 5, 6, 7]
list2 = []
for a, b in zip(list1[:-1], list1[1:]):
    difference = b – a
    list2.append(difference)
print(list2)

a) [1, 2, 1, 0, 1, 1, 1]
b) [0, 2, 1, 0, 1, 1, 1]
c) [1, 0, 1, 0, 1, 1, 1]
d) [0, 1, 1, 1, 1, 1]

Correct answer is: c) [1, 0, 1, 0, 1, 1, 1]
Explanation: The code calculates the difference between consecutive elements in `list1` and stores them in `list2`. The differences are: 1-1=0, 3-1=2, 4-3=1, 4-4=0, 5-4=1, 6-5=1, 7-6=1, resulting in the list `[1, 0, 1, 0, 1, 1, 1]`.

25). What is the purpose of the following code snippet?
list1 = [3, 5, 7, 2, 6, 12, 3]
total = 0

for value in list1:
    total += value
print(“Average of list: “,total/len(list1))

a) It calculates the sum of all the values in the list.
b) It calculates the average of all the values in the list.
c) It finds the maximum value in the list.
d) It counts the number of occurrences of a specific value in the list.

Correct answer is: b) It calculates the average of all the values in the list.
Explanation: The code snippet iterates through each value in the list and accumulates the sum of all the values in the variable ‘total’. Then, it calculates the average by dividing the total sum by the length of the list and prints it.

Leave a Comment