Exercise: Python

Questions for: Lists

How can you find the maximum value in a list in Python?
A:
max_value(list)
B:
list.max()
C:
max(list)
D:
list.find_max()
Answer: C
The max() function is used to find the maximum value in a list in Python.
What is the purpose of the list() constructor in Python?
A:
Creates an empty list.
B:
Converts a tuple to a list.
C:
Converts a string to a list.
D:
All of the above.
Answer: D
The list() constructor in Python can be used to create an empty list, convert a tuple to a list, and convert a string to a list.
What does the reverse() method do 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.
What is the difference between the remove and pop methods in Python lists?
A:
remove removes by value, pop removes by index.
B:
remove removes by index, pop removes by value.
C:
Both remove and pop remove by index.
D:
Both remove and pop remove by value.
Answer: A
The remove method removes an element by value, while the pop method removes an element by index.
How can you remove the last element from a list in Python?
A:
list.remove(-1)
B:
list.pop()
C:
list.delete(-1)
D:
list.trim()
Answer: B
The pop() method is used to remove the last element from a list in Python.
Ad Slot (Above Pagination)
Quiz