Python List MCQ : Set 4

Python List MCQ

1). Which method can be used as an alternative to the min() function to find the minimum value from a list?

a) sort()
b) reverse()
c) pop()
d) append()

Correct answer is: a) sort()
Explanation: The sort() method can be used to sort the list in ascending order, and then the first element will be the minimum value.

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

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

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

Correct answer is: b) [1, 2, 4, 5]
Explanation: The del keyword is used to delete an element from a list by its index. In this case, del my_list[2] removes the element at index 2 (which is 3) from my_list. The resulting list is [1, 2, 4, 5].

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

string = “i am learning python”
list1 = string.split(” “)
print(list1)

a) [“i”, “am”, “learning”, “python”]
b) [“i am learning python”]
c) [“i”, “am”, “learning”, “p”, “y”, “t”, “h”, “o”, “n”]
d) [“i”, “,”, “am”, “,”, “learning”, “,”, “python”]

Correct answer is: a) [“i”, “am”, “learning”, “python”]
Explanation: The split(” “) method splits the string at each occurrence of a space, resulting in a list of words.

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

list1 = [“Hello”, 45, “sqa”, 23, 5, “Tools”, 20]
count = 0

for value in list1:
    if isinstance(value, int):
        count += 1
print(“Total number of integers: “, count)

a) Total number of integers: 4
b) Total number of integers: 3
c) Total number of integers: 5
d) Total number of integers: 2

Correct answer is: a) Total number of integers: 4
Explanation: The code iterates over each element in `list1` and checks if the element is an instance of the `int` type using the `isinstance()` function. If it is an integer, the `count` variable is incremented. The final value of `count` is printed, which represents the total number of integers in the list. In this case, the integers in the list are 45, 23, 5, and 20, resulting in a count of 4.

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

list1 = [2, 3, 4, 7, 8, 1, 5, 6, 2, 1, 8, 2]
index_list = [0, 3, 5, 6]
new_list = []

for value in index_list:
    new_list.append(list1[value])
print(new_list)

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

Correct answer is: a) [2, 7, 1, 5]
Explanation: The code iterates through each value in the `index_list` and appends the corresponding element from `list1` to `new_list`.
– For `value = 0`, the element at index 0 in `list1` is 2, so 2 is appended to `new_list`.
– For `value = 3`, the element at index 3 in `list1` is 7, so 7 is appended to `new_list`.
– For `value = 5`, the element at index 5 in `list1` is 1, so 1 is appended to `new_list`.
– For `value = 6`, the element at index 6 in `list1` is 5, so 5 is appended to `new_list`.
Therefore, the resulting `new_list` is `[2, 7, 1, 5]`.

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

list1 = [{“name”: “john”}, {“city”: “mumbai”}, {“Python”: “laguage”}, {“name”: “john”}]
list2 = []

for i in range(len(list1)):
    if list1[i] not in list1[i+1:]:
        list2.append(list1[i])
print(list2)

a) `[{‘city’: ‘mumbai’}, {‘Python’: ‘laguage’}, {‘name’: ‘john’}]`
b) `[{“name”: “john”}, {“Python”: “laguage”}]`
c) `[{“name”: “john”}]`
d) `[]`

Correct answer is: a) `[{‘city’: ‘mumbai’}, {‘Python’: ‘laguage’}, {‘name’: ‘john’}]`
Explanation: The code iterates over each element in `list1` using the `for` loop. The condition `if list1[i] not in list1[i+1:]` checks if the current element is not repeated in the remaining elements of `list1`. If it is not repeated, the element is appended to `list2`.

7). What is the purpose of the join() method in the Python list?

a) It joins the elements of the list into a single string.
b) It counts the number of occurrences of a specified element in the list.
c) It reverses the order of the elements in the list.
d) It converts the string into a list.

Correct answer is: a) It joins the elements of the list into a single string.
Explanation: The join() method is used to concatenate the elements of the list into a single string.

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

list1 = [“a”, “b”, “c”, “d”, “e”]
list2 = [234, 123, 456, 343, 223]

dictionary = dict(zip(list1, list2))
print(dictionary)

a) {‘a’: 234, ‘b’: 123, ‘c’: 456, ‘d’: 343, ‘e’: 223}
b) {‘a’: 123, ‘b’: 234, ‘c’: 343, ‘d’: 456, ‘e’: 223}
c) {234: ‘a’, 123: ‘b’, 456: ‘c’, 343: ‘d’, 223: ‘e’}
d) {1: ‘a’, 2: ‘b’, 3: ‘c’, 4: ‘d’, 5: ‘e’}

Correct answer is: a) {‘a’: 234, ‘b’: 123, ‘c’: 456, ‘d’: 343, ‘e’: 223}
Explanation: The zip() function is used to combine the elements of list1 and list2 into pairs. The dict() function then converts these pairs into a dictionary where elements from list1 serve as keys and elements from list2 serve as values. The resulting dictionary is printed, which will be {‘a’: 234, ‘b’: 123, ‘c’: 456, ‘d’: 343, ‘e’: 223}.

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

list1 = [2, 5, 7, 8, 2, 3, 4, 12, 5, 6]
list2 = list(set(list1))

print(list2)

a) [2, 5, 7, 8, 2, 3, 4, 12, 5, 6]
b) [2, 5, 7, 8, 3, 4, 12, 6]
c) [2, 5, 7, 8, 3, 4, 6, 12]
d) [2, 3, 4, 5, 6, 7, 8, 12]

Correct answer is: c) [2, 5, 7, 8, 3, 4, 6, 12]
Explanation: In the code, `list1` contains duplicate elements. The `set()` function is used to convert `list1` into a set, which automatically removes duplicate elements. Then, `list()` is used to convert the set back into a list. The resulting `list2` will contain unique elements from `list1` in an arbitrary order. Therefore, the output is `[2, 5, 7, 8, 3, 4, 6, 12]`.

10). Which function is used to find the maximum value from a list?

a) min()
b) max()
c) sum()
d) len()

Correct answer is: B) max()
Explanation: The max() function is used to find the maximum value from a list. When called with a list as an argument, it returns the largest value present in that list.

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

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

a) [4, 6, 8, [5, 2, 6], 2, 3, 5]
b) [4, 6, 8, 5, 2, 6, 2, 3, 5]
c) [4, 6, 8, [5, 2, 6], [5, 2, 6], 2, 3, 5]
d) Error: ‘list’ object cannot be inserted into another list

Correct answer is: a) [4, 6, 8, [5, 2, 6], 2, 3, 5]
Explanation: The `insert()` method is used to insert an element into a list at a specified index. In this case, the list `[5, 2, 6]` is inserted at index 3 of `list1`. The resulting list will be `[4, 6, 8, [5, 2, 6], 2, 3, 5]`, where the sublist `[5, 2, 6]` is treated as a single element in `list1`.

12). What is the output of the following code?
list1 = [[“apple”, 30], [“mango”, 50], [“banana”, 20], [“lichi”, 50]]
list2 = [[“apple”, 2],[“mango”,10]]

for value in list1:
    for var in list2:
        if value[0] == var[0]:
            print(“Fruit: “, value[0])
            print(“Bill: “, value[1]*var[1])

a) Fruit: apple
Bill: 60
Fruit: mango
Bill: 500
b) Fruit: apple
Bill: 600
Fruit: mango
Bill: 500
c) Fruit: apple
Bill: 120
Fruit: mango
Bill: 500
d) Fruit: apple
Bill: 60
Fruit: mango
Bill: 50

Correct answer is: a) Fruit: apple
Bill: 60
Fruit: mango
Bill: 500

Explanation: The code iterates over the elements of list1 and list2. When it finds a match between the fruits in both lists (apple and mango), it prints the fruit name and the multiplication of their respective quantities. In this case, “apple” has a quantity of 2 in list2 and a price of 30 in list1, resulting in a bill of 60. Similarly, “mango” has a quantity of 10 in list2 and a price of 50 in list1, resulting in a bill of 500.

13). What is the output of the following code?
list1 = [80, 50, 70, 90, 95]
print(“Percentage: “, (sum(list1)/500)*100)

a) Percentage: 75.0
b) Percentage: 85.0
c) Percentage: 90.0
d) Percentage: 95.0

Correct answer is: b) Percentage: 85.0
Explanation: The code calculates the average of the values in list1 by summing them up and dividing by 500, and then multiplying by 100 to get the percentage. The sum of [80, 50, 70, 90, 95] is 385, and (385/500)*100 equals 77.0.

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

list1 = [“data”, “python”, “oko”, “test”, “ete”]
list2 = []

for value in list1:
    new = value[::-1]
    if new == value:
        list2.append(value)
print(list2)

a) [“oko”, “ete”]
b) [“data”, “python”, “test”]
c) [“oko”, “test”]
d) [“data”, “python”, “oko”, “test”, “ete”]

Correct answer is: a) [“oko”, “ete”]
Explanation: The code checks each element in `list1` and appends it to `list2` if it is a palindrome (reads the same forwards and backward). In this case, “oko” and “ete” are palindromes, so they are added to `list2`, resulting in `list2` containing [“oko”, “ete”].

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

list1 = [0, 12, [22, 32], 42, 52, [62, 72, 82], [92, 102, 112, 122]]
list2 = []

for value in list1:
    if type(value) is list:
        for var in value:
            list2.append(var)
    else:
        list2.append(value)
print(list2)

a) [0, 12, [22, 32], 42, 52, [62, 72, 82], [92, 102, 112, 122]]
b) [0, 12, 22, 32, 42, 52, 62, 72, 82, 92, 102, 112, 122]
c) [0, 12, [22, 32], 42, 52, [62, 72, 82], [92, 102, 112, 122], 22, 32, 62, 72, 82, 92, 102, 112, 122]
d) [0, 12, [22, 32], 42, 52, [62, 72, 82], [92, 102, 112, 122], [22, 32], [62, 72, 82], [92, 102, 112, 122]]

Correct answer is: b) [0, 12, 22, 32, 42, 52, 62, 72, 82, 92, 102, 112, 122]
Explanation: The code iterates through each element in `list1`. If an element is of type list, it iterates through its sub-elements and appends them to `list2`. If an element is not a list, it directly appends it to `list2`. The final output is the flattened list with all the sub-elements combined.

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

list1 = [(3, 5), (6, 8), (8, 11), (12, 14), (17, 23)]
list2 = []

for value in list1:
    if type(value) is tuple:
        list3 = []
        for var in value:
            list3.append(var)
        list2.append(list3)
print(list2)

a) [[3, 5], [6, 8], [8, 11], [12, 14], [17, 23]]
b) [(3, 5), (6, 8), (8, 11), (12, 14), (17, 23)]
c) [[(3, 5)], [(6, 8)], [(8, 11)], [(12, 14)], [(17, 23)]]
d) [(3, 5), 6, 8, 8, 11, 12, 14, 17, 23]

Correct answer is: a) [[3, 5], [6, 8], [8, 11], [12, 14], [17, 23]]
Explanation: The code iterates over each value in list1. If the value is a tuple, it creates a new list (list3) and appends each element of the tuple to list3. Finally, list3 is appended to list2. The output is a list of lists, where each tuple in list1 is converted to a list.

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

list1 = [[“a”, 5], [“b”, 8], [“c”, 11], [“d”, 14], [“e”, 23]]

dictionary = dict(list1)
print(dictionary)

a) {“a”: 5, “b”: 8, “c”: 11, “d”: 14, “e”: 23}
b) {“a”: [5], “b”: [8], “c”: [11], “d”: [14], “e”: [23]}
c) {“a”: [“a”, 5], “b”: [“b”, 8], “c”: [“c”, 11], “d”: [“d”, 14], “e”: [“e”, 23]}
d) Error: Invalid syntax

Correct answer is: a) {“a”: 5, “b”: 8, “c”: 11, “d”: 14, “e”: 23}
Explanation: The code converts the list of lists, `list1`, into a dictionary using the `dict()` function. Each inner list in `list1` represents a key-value pair in the dictionary. The resulting dictionary contains the keys from the first elements of each inner list and the corresponding values from the second elements of each inner list. Thus, the output is `{“a”: 5, “b”: 8, “c”: 11, “d”: 14, “e”: 23}`.

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

list1 = [“Hello”, “student”, “are”, “learning”, “Python”, “Its”, “Python”, “Language”]
for i in range(len(list1)):
    if list1[i] == “Python”:
        list1[i] = “Java”

print(list1)

a) [“Hello”, “student”, “are”, “learning”, “Java”, “Its”, “Java”, “Language”]
b) [“Hello”, “student”, “are”, “learning”, “Python”, “Its”, “Python”, “Language”]
c) [“Hello”, “student”, “are”, “learning”, “Java”, “Its”, “Python”, “Language”]
d) [“Hello”, “student”, “are”, “learning”, “Java”, “Its”, “Java”, “Language”]

Correct answer is: a) [“Hello”, “student”, “are”, “learning”, “Java”, “Its”, “Java”, “Language”]
Explanation: The code iterates over each element in the list using the range(len(list1)) construct. If an element is equal to “Python”, it is replaced with “Java”. After the loop, the modified list is printed, which will have “Python” replaced with “Java” at two positions.

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

list1 = [“Hello”, “student”, “are”, “learning”, “Python”, “Its”, “Python”, “Language”]
dictionary = dict()
list2 = []

for value in list1:
    dictionary[value] = len(value)
list2.append(dictionary)

print(list2)

a) [{‘Hello’: 5, ‘student’: 7, ‘are’: 3, ‘learning’: 8, ‘Python’: 6, ‘Its’: 3, ‘Language’: 8}]
b) [{‘Hello’: 5, ‘student’: 7, ‘are’: 3, ‘learning’: 8, ‘Python’: 6, ‘Its’: 3, ‘Python’: 6, ‘Language’: 8}]
c) [{‘Hello’: 5}, {‘student’: 7}, {‘are’: 3}, {‘learning’: 8}, {‘Python’: 6}, {‘Its’: 3}, {‘Python’: 6}, {‘Language’: 8}]
d) [{‘Hello’: 5, ‘student’: 7, ‘are’: 3, ‘learning’: 8, ‘Python’: 6, ‘Its’: 3}, {‘Python’: 6, ‘Language’: 8}]

Correct answer is: a) [{‘Hello’: 5, ‘student’: 7, ‘are’: 3, ‘learning’: 8, ‘Python’: 6, ‘Its’: 3, ‘Language’: 8}]
Explanation: The code creates a dictionary called `dictionary` and a list called `list2`. It then iterates through each value in `list1` and assigns the length of the value as the value in the `dictionary` with the value itself as the key. Finally, the `dictionary` is appended to `list2`. The output will be a list containing a single dictionary with key-value pairs mapping each word from `list1` to its corresponding length.

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

list1 = [2, 5, 7, 9, 3, 4]
print(“Remove 1 element from left”, list1[1:])

a) Remove 1 element from left [5, 7, 9, 3, 4]
b) Remove 1 element from left [2, 5, 7, 9, 3]
c) Remove 1 element from left [2, 5, 7, 9, 3, 4]
d) Remove 1 element from left [5, 7, 9, 3, 4, 2]

Correct answer is: a) Remove 1 element from left [5, 7, 9, 3, 4]
Explanation: `list1[1:]` returns a new list starting from the second element (index 1) of `list1`. The output will be [5, 7, 9, 3, 4], as the first element 2 is removed from the left side of the list.

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

list1 = []
num1 = 0
num2 = 1

for i in range(1, 10):
    list1.append(num1)
    n2 = num1 + num2
    num1 = num2
    num2 = n2

print(list1)

a) [0, 1, 1, 2, 3, 5, 8, 13, 21]
b) [1, 1, 2, 3, 5, 8, 13, 21, 34]
c) [0, 1, 2, 3, 4, 5, 6, 7, 8]
d) [0, 0, 1, 1, 2, 3, 5, 8, 13]

Correct answer is: a) [0, 1, 1, 2, 3, 5, 8, 13, 21]
Explanation: The code snippet initializes an empty list `list1` and two variables `num1` and `num2` to 0 and 1, respectively. It then enters a loop where it appends the value of `num1` to `list1` and updates `num1` and `num2` according to the Fibonacci sequence. The loop runs 9 times, resulting in the Fibonacci numbers [0, 1, 1, 2, 3, 5, 8, 13, 21]. Finally, the `list1` is printed, displaying the Fibonacci sequence.

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

list1 = [“python”, “is”, “a”, “best”, “language”, “python”, “best”]
list2 = []

for words in list1:
    if words not in list2:
        list2.append(words)
print(list2)

a) [“python”, “is”, “a”, “best”, “language”]
b) [“python”, “is”, “a”, “best”, “language”, “python”]
c) [“python”, “is”, “a”, “best”, “language”, “python”, “best”]
d) [“is”, “a”, “language”]

Correct answer is: a) [“python”, “is”, “a”, “best”, “language”]
Explanation: The code iterates over each word in list1 and checks if it exists in list2. If it doesn’t exist, it appends the word to list2. Since “python” and “best” already exist in list2, they are not appended again. Therefore, the final output is [“python”, “is”, “a”, “best”, “language”].

23). What is the output of the following code?
list1 = [(2, 3), (4, 6), (5, 1), (2, 3), (7, 9), (5, 1)]
list2 = []

for value in list1:
    if value not in list2:
        list2.append(value)
print(list2)

a) []
b) [(2, 3), (4, 6), (5, 1), (7, 9)]
c) [(2, 3), (4, 6), (5, 1), (2, 3), (7, 9), (5, 1)]
d) Error: list index out of range

Correct answer is: b) [(2, 3), (4, 6), (5, 1), (7, 9)]
Explanation: The code iterates over the elements in list1 and checks if each element is already present in list2. If not, it appends the element to list2. However, since list2 starts as an empty list and the loop does not execute, list2 remains empty. Therefore, the output is [(2, 3), (4, 6), (5, 1), (7, 9)].

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

list1 = [[1, 2], [3, 5], [1, 2], [6, 7]]
list2 = []

for value in list1:
    if value not in list2:
        list2.append(value)
print(list2)

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

Correct answer is: b) [[1, 2], [3, 5], [6, 7]]
Explanation: The code iterates over each element in `list1` and checks if the element is already present in `list2`. Since the sublists `[1, 2]`, `[3, 5]`, and `[6, 7]` are not present in `list2`, they are appended to it. Thus, `list2` contains all unique elements from `list1`.

25). 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=max))

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

Correct answer is: c) [[1, 2, 1], [2, 1, 3], [0, 4, 1], [5, 1, 1], [3, 5, 6]]
Explanation: The `sorted()` function is used to sort the elements of `list1` based on the maximum value within each sublist. The `key=max` argument specifies that the maximum value of each sublist should be used as the key for sorting. Therefore, the resulting list will be sorted in ascending order based on the maximum value of each sublist. In this case, the sorted list will be `[[1, 2, 1], [2, 1, 3], [0, 4, 1], [5, 1, 1], [3, 5, 6]]`.

Leave a Comment