Python Programming - Lists

Exercise : Lists - General Questions
  • Lists - General Questions
26.
How can you check if a list is empty in Python?
if list.is_empty()
if list == []
if not list:
if len(list) == 0
Answer: Option
Explanation:
Checking if not list: is a common way to check if a list is empty in Python.

27.
What is the purpose of the reverse() method in Python lists?
Sorts the list in descending order.
Reverses the order of elements in the list.
Removes duplicate elements from the list.
Appends the list to itself.
Answer: Option
Explanation:
The reverse() method in Python lists is used to reverse the order of elements in-place.

28.
How can you find the sum of all elements in a list in Python?
sum(list)
list.total()
list.sum()
total(list)
Answer: Option
Explanation:
The sum() function is used to find the sum of all elements in a list in Python.

29.
What will the list.remove(42) method do?
Removes the element at index 42.
Removes the element with the value 42.
Raises an error because index 42 does not exist.
Removes all occurrences of the value 42.
Answer: Option
Explanation:
The remove() method is used to remove the first occurrence of a specific value in the list.

30.
What does the count() method of a list return in Python?
The total number of elements in the list.
The number of occurrences of a specific element.
The sum of all elements in the list.
The index of the last occurrence of a specific element.
Answer: Option
Explanation:
The count() method returns the number of occurrences of a specific element in a list.