Python List MCQ
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.