MCQ
Python Lists
Test your knowledge of Python lists — one of the most commonly used data structures in Python.
1. Which of the following correctly creates an empty list in Python?
✅ Correct Answer
Square brackets `[]` are used to create a list in Python.
2. What will be the output of the following code?
✅ Correct Answer
`len(my_list)` returns the number of elements in the list, which is 3.
3. Which of the following statements adds an element to the end of a list?
✅ Correct Answer
The `append()` method adds an item to the end of the list.
4. What will be the output of the following code?
✅ Correct Answer
List indices start from 0, so `nums[2]` refers to the third element, which is 30.
5. Which method removes all elements from a list?
✅ Correct Answer
The `clear()` method removes all elements from a list, leaving it empty.
6. What is the output of the following code?
✅ Correct Answer
Both `a` and `b` refer to the same list object. Appending to `b` also changes `a`.
7. How can you get the last element of a list in Python?
✅ Correct Answer
Negative indexing allows access from the end; `list[-1]` gives the last element.
8. What will the following code print?
✅ Correct Answer
List slicing includes the start index but excludes the end index, so `[1:3]` gives `[2, 3]`.
9. Which of the following methods combines two lists?
✅ Correct Answer
Both `+` and `extend()` can be used to combine lists — `+` creates a new list while `extend()` modifies the original.
10. What is the result of the following code?
✅ Correct Answer
`insert(1, 20)` adds 20 at index 1, pushing existing elements to the right, resulting in `[5, 20, 10, 15]`.
11. What will be the output of the following code?
✅ Correct Answer
Negative indexing starts from the end. `nums[-1]` is 40 and `nums[-2]` is 30.
12. Which of the following methods adds an element to the end of a list?
✅ Correct Answer
The `append()` method adds a single element to the end of a list.
13. What will be the output of the following code?
✅ Correct Answer
The `+` operator concatenates two lists in Python, not adds their elements.
14. What is the output of the following code?
✅ Correct Answer
`remove(2)` deletes the first occurrence of the value `2` from the list.
15. Which method is used to sort a list in ascending order?
✅ Correct Answer
The `sort()` method sorts the list in place by default.
16. What will be printed?
✅ Correct Answer
The `in` keyword checks if an element exists in a list. 'banana' is indeed in the list.
17. What is the output of the following code?
✅ Correct Answer
Slicing `[1:4]` returns elements from index 1 to 3 (4 is excluded).
18. Which statement creates a new list containing numbers 1 to 5?
✅ Correct Answer
Square brackets `[]` define a list. Parentheses define a tuple, and curly braces define a set or dict.
19. What will this code print?
✅ Correct Answer
`append()` adds the argument as a single element — so the list `[4,5]` is added as a sublist.
20. What is the difference between `append()` and `extend()` in Python lists?
✅ Correct Answer
`append()` adds its argument as a single item, whereas `extend()` iterates over its argument and adds each element to the list.