Exercise: Lists

Questions for: Lists

What does the count() method of a list return in Python?
A:
The total number of elements in the list.
B:
The number of occurrences of a specific element.
C:
The sum of all elements in the list.
D:
The index of the last occurrence of a specific element.
Answer: B
The count() method returns the number of occurrences of a specific element in a list.
What will the list.remove(42) method do?
A:
Removes the element at index 42.
B:
Removes the element with the value 42.
C:
Raises an error because index 42 does not exist.
D:
Removes all occurrences of the value 42.
Answer: B
The remove() method is used to remove the first occurrence of a specific value in the list.
How can you find the sum of all elements in a list in Python?
A:
sum(list)
B:
list.total()
C:
list.sum()
D:
total(list)
Answer: A
The sum() function is used to find the sum of all elements in a list in Python.
What is the purpose of the reverse() method in Python lists?
A:
Sorts the list in descending order.
B:
Reverses the order of elements in the list.
C:
Removes duplicate elements from the list.
D:
Appends the list to itself.
Answer: B
The reverse() method in Python lists is used to reverse the order of elements in-place.
How can you check if a list is empty in Python?
A:
if list.is_empty()
B:
if list == []
C:
if not list:
D:
if len(list) == 0
Answer: C
Checking if not list: is a common way to check if a list is empty in Python.
Ad Slot (Above Pagination)
Quiz