Lists are one of Python's most versatile and commonly used data types. They are ordered collections of items (or elements), which can be of any type. Test your understanding of Python lists with these 15 Multiple Choice Questions.
1. How do you create an empty list in Python?
a) list = ()
b) list = []
c) list = {}
d) list = list()
Answer:
b) list = []
Explanation:
An empty list in Python is created using square brackets [].
2. How do you add an item to the end of a list named 'myList'?
a) myList.add(item)
b) myList.append(item)
c) myList.insert(item)
d) myList.addEnd(item)
Answer:
b) myList.append(item)
Explanation:
The append() method is used to add an item to the end of a list.
3. What does the 'pop' method do in a list?
a) Removes and returns the last item
b) Removes an item at a specific index
c) Removes all items from the list
d) Removes the first item
Answer:
a) Removes and returns the last item
Explanation:
The pop() method removes and returns the last item of the list. If an index is specified, it removes and returns the item at that index.
4. How do you find the number of items in a list named 'myList'?
a) myList.count()
b) len(myList)
c) myList.length()
d) count(myList)
Answer:
b) len(myList)
Explanation:
The len() function is used to find the number of items in a list.
5. How do you concatenate two lists, list1 and list2?
a) list1 + list2
b) list1.concat(list2)
c) list1.append(list2)
d) list1.extend(list2)
Answer:
a) list1 + list2
Explanation:
The + operator is used to concatenate two lists in Python.
6. What is the output of this code? my_list = [1, 2, 3]; print(my_list[1])
a) 1
b) 2
c) 3
d) Error
Answer:
b) 2
Explanation:
List indices start at 0, so my_list[1] refers to the second item in the list.
7. How do you insert an item at a specific index in a list?
a) list.insert(item, index)
b) list.add(item, index)
c) list.insert(index, item)
d) list.add(index, item)
Answer:
c) list.insert(index, item)
Explanation:
The insert() method inserts an item at the specified index in the list.
8. How do you remove an item by its value from a list named 'myList'?
a) myList.remove(item)
b) myList.delete(item)
c) del myList[item]
d) myList.pop(item)
Answer:
a) myList.remove(item)
Explanation:
The remove() method removes the first occurrence of the specified item.
9. What does myList[-1] return?
a) The first item of the list
b) The last item of the list
c) The second to last item of the list
d) An error
Answer:
b) The last item of the list
Explanation:
Negative indices start from the end of the list, with -1 being the last item.
10. How do you create a list with the same item repeated multiple times?
a) [item] * times
b) [item, times]
c) [item * times]
d) multiply(item, times)
Answer:
a) [item] * times
Explanation:
In Python, multiplying a list by a number n creates a new list with the original elements repeated n times.
11. How do you reverse the items in a list named 'myList'?
a) myList.reverse()
b) reverse(myList)
c) myList.reversed()
d) myList.sort(reverse=True)
Answer:
a) myList.reverse()
Explanation:
The reverse() method reverses the elements of the list in place.
12. How do you get a slice of a list 'myList' from index 2 to 4 (inclusive)?
a) myList[2:5]
b) myList[2:4]
c) myList.slice(2, 4)
d) myList.get(2, 4)
Answer:
a) myList[2:5]
Explanation:
Slicing a list with myList[start:end] returns a new list from the start index to end-1.
13. How do you check if an item is in a list named 'myList'?
a) myList.contains(item)
b) item in myList
c) myList.has(item)
d) in myList(item)
Answer:
b) item in myList
Explanation:
The 'in' keyword is used to check if an item exists in a list.
14. What does the 'sort' method do in a list?
a) Sorts the list in descending order
b) Sorts the list in ascending order
c) Randomly shuffles the list
d) Reverses the list
Answer:
b) Sorts the list in ascending order
Explanation:
The sort() method sorts the elements of the list in ascending order by default.
15. How do you copy a list named 'myList'?
a) newList = myList
b) newList = myList.copy()
c) newList = copy(myList)
d) newList = list(myList)
Answer:
b) newList = myList.copy()
Explanation:
The copy() method is used to make a shallow copy of the list.
Comments
Post a Comment