Exercise: Python

Questions for: Lists

Which function is used to create a shallow copy of a list in Python?
A:
copy()
B:
clone()
C:
duplicate()
D:
replicate()
Answer: A
The copy() function is used to create a shallow copy of a list in Python.
What does the index method of a list return in Python?
A:
The first occurrence of the specified element.
B:
The last occurrence of the specified element.
C:
The index of the specified element.
D:
The count of occurrences of the specified element.
Answer: C
The index method returns the index of the first occurrence of the specified element in a list.
What is the correct way to sort a list in descending order in Python?
A:
list.sort()
B:
sorted(list, reverse=True)
C:
list.sort(reverse=True)
D:
sorted(list)
Answer: C
The list.sort(reverse=True) method is used to sort a list in descending order in-place.
Which method is used to add elements from one list to another in Python?
A:
concat()
B:
merge()
C:
extend()
D:
append()
Answer: C
The extend() method is used to add elements from one list to the end of another list.
How do you remove an element from a list in Python?
A:
remove(element)
B:
pop(index)
C:
delete(element)
D:
discard(element)
Answer: B
The pop(index) method is used to remove and return the element at the specified index in a list.
Ad Slot (Above Pagination)
Quiz