5.30. Functions with Lists Multiple Choice QuestionsΒΆ
- [-2, 0, 1, 4.2, 5.2]
- Incorrect. This function will sort the list in order from lowest to highest, and then add the sum to the end of the list.
- [-2, 0, 1, 5.2, 4.2]
- Correct! It sorts the list in ascending order and then adds the sum at the end.
- [4.2, -2, 0, 1, 5.2]
- Incorrect. This function will sort the list in order from lowest to highest, and then add the sum to the end of the list.
- [0, -2, 5.2, 1, 4.2]
- Incorrect. This function will sort the list in order from lowest to highest, and then add the sum to the end of the list.
- [5.2, 1, 0, -2, 4.2]
- Incorrect. This function will sort the list in order from lowest to highest, and then add the sum to the end of the list.
Q-1: What would list_transformation([0, -2, 5.2, 1])
return?
def list_transformation(lst):
lst.sort()
sum_of_lst = sum(lst)
lst.append(sum_of_lst)
return lst
- 0
- Correct! This will remove the number at index 0.
- 2.5
- Incorrect! The built-in function, pop, removes the value from a list, lst, at a specified index, num.
- 4
- Incorrect! The built-in function, pop, removes the value from a list, lst, at a specified index, num.
- -5
- Incorrect! The built-in function, pop, removes the value from a list, lst, at a specified index, num.
- -4
- Correct! This will remove the number at index 0.
Q-2: Which values of num
would not return an error if lst
were [0, 4, -4, 2.5]
?
def popping_removal(lst, num):
lst.pop(num)
return lst
- [0, 200, 5.6, -2, 4, 5, [-2, 4, 5], -2, 4, 5]
- Incorrect! The built-in function append adds a list to the end of a list. The built-in function extend adds the values of a list to the end of a list.
- [0, 200, 5.6, -2, 4, 5, 5.6, -2, 4, 5.6, -2, 4]
- Incorrect! The built-in function append adds a list to the end of a list. The built-in function extend adds the values of a list to the end of a list.
- [0, 200, 5.6, -2, 4, 5, [5.6, -2, 4], 5.6, -2, 4]
- Correct! If the item you are appending is a list it is added as a list, while extend adds the values in the list.
- [0, 200, 5.6, -2, 4, 5, 5.6, -2, 4, [5.6, -2, 4]]
- Incorrect! The built-in function append adds a list to the end of a list. The built-in function extend adds the values of a list to the end of a list.
- [0, 200, 5.6, -2, 4, 5, -2, 4, 5, [-2, 4, 5]]
- Incorrect! The built-in function append adds a list to the end of a list. The built-in function extend adds the values of a list to the end of a list.
Q-3: What would the function extend_and_append
output if lst
were [0, 200, 5.6, -2, 4, 5]
?
def extend_and_append(lst):
new_list = lst[-4:-1]
lst.append(new_list)
lst.extend(new_list)
return lst
- elif len(lst1) = len(lst2):
- Incorrect! A single equal sign is the assignment operator.
- elif len(lst1) == len(lst2):
- Correct! This checks if they are the same length.
- elif not(len(lst1) > len(lst2)) and not(len(lst1) < len(lst2))
- Correct! This checks that the length of lst1 is not greater or less than lst2.
- elif not(len(lst1) > len(lst2)) or not(len(lst1) < len(lst2))
- Incorrect! When an or operator is used with two statements, only one of the statements has to be true in order for the entire statement to pass as true.
- elif not(len(lst1) is not len(lst2)):
- Correct! This checks if it is not true that the len of lst1 is not the same as the length of lst2.
Q-4: Which of the following would correctly replace the else statement?
def comparing_lengths(lst1, lst2):
if len(lst1) > len(lst2):
return "The length of lst1 is greater than the length of lst1"
elif len(lst1) < len(lst2):
return "The length of lst2 is greater than the length of lst1"
else:
return "The length of lst1 and the length of lst2 are equal"
- lst_reverse = lst.reverse(), return lst
- Correct! Although it properly returns a reversed list, it is unnecessary to assign the reverse method to a variable. Reverse returns None.
- lst.reverse(), return lst
- Correct! This will reverse the list and then return it. Reverse returns None.
- lst_reverse = lst.reverse(), return lst_reverse
- Incorrect! The built-in python function, reverse, does not need to be assigned to a new variable.
- return lst.reverse()
- Incorrect! The built-in python function, reverse, has to be used on the list before the list can be returned.
- lst_reverse = lst_reverse.reverse(), return lst
- Incorrect! This code does not modify lst, the passed in parameter.
Q-5: Which of the following line(s) of code would properly return a reversed list? (Note: Commas represent line breaks)
def reverse_list(lst):
# line(s)
You have attempted of activities on this page