Welcome to the Python Lists Quiz for beginners! This quiz is perfect for those starting their journey in Python and wanting to test their understanding of Python lists. Lists are versatile and are used extensively in Python programming. They are great for storing an ordered collection of items, which can be of varied types. Let's dive in and check your knowledge of Python lists!
1. What is a list in Python?
Answer:
Explanation:
In Python, a list is an ordered collection of elements that can be changed and indexed. It allows duplicate elements.
2. How do you create a list in Python?
Answer:
Explanation:
Lists in Python are created by placing elements inside square brackets [], separated by commas.
3. How can you add an element to the end of a list in Python?
Answer:
Explanation:
The append() method adds a single element to the end of a list.
4. How do you access the third element of a list named 'myList'?
Answer:
Explanation:
List indices start at 0, so the third element is accessed with myList[2].
5. What does the following list slicing indicate? myList[1:4]
Answer:
Explanation:
List slicing myList[1:4] includes elements from index 1 up to, but not including, index 4.
6. Which method removes the last element from a list?
Answer:
Explanation:
The pop() method removes and returns the last element from the list.
7. How do you change the value of the first element in a list named 'myList' to 'new_value'?
Answer:
Explanation:
You can change the value of an element by accessing it via its index and assigning a new value.
8. What is the output of len([10, 20, 30])?
Answer:
Explanation:
The len() function returns the number of elements in a list, which is 3 in this case.
9. How do you concatenate two lists, list1 and list2?
Answer:
Explanation:
The + operator is used to concatenate two lists.
10. What does myList.insert(2, 'new_element') do?
Answer:
Explanation:
The insert() method inserts an element at the specified index without replacing the existing element.
11. What type of elements can a Python list contain?
Answer:
Explanation:
Python lists can contain elements of any data type, including mixed types.
12. What is the result of the expression 3 * [1, 2, 3]?
Answer:
Explanation:
Multiplying a list by an integer n replicates its elements n times.
13. What does the remove() method do in a list?
Answer:
Explanation:
The remove() method removes the first occurrence of the element specified.
14. What is the output of the following code?
myList = [1, 2, 3]
myList.extend([4, 5])
print(myList)
Answer:
Explanation:
The extend() method adds all elements of the specified list to the end of the current list.
15. How do you create a nested list in Python?
Answer:
Explanation:
Nested lists are created by placing a list as an element inside another list.
16. What will be the result of the following code?
myList = [1, 2, 3]
del myList[1]
print(myList)
Answer:
Explanation:
The del statement removes the element at the specified index from the list.
17. What does list slicing myList[::2] return?
Answer:
Explanation:
The slicing myList[::2] returns every second element of the list starting from the first element (index 0).
18. How do you reverse the elements of a list in-place?
Answer:
Explanation:
The reverse() method reverses the elements of the list in-place.
19. What is the correct way to create a copy of a list named 'myList'?
Answer:
Explanation:
Both myList.copy() and list(myList) create a shallow copy of the list.
20. What will be the output of the following code?
myList = ['a', 'b', 'c', 'd']
print(myList[-1])
Answer:
Explanation:
Negative indices count from the end of the list, so myList[-1] refers to the last element 'd'.
Comments
Post a Comment
Leave Comment