Pythondex

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?

list = ()
list = {}
list = []
list = ''

✅ Correct Answer

Square brackets `[]` are used to create a list in Python.

2. What will be the output of the following code?

2
3
4
Error

✅ 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?

list.add(5)
list.append(5)
list.insert(5)
list.push(5)

✅ Correct Answer

The `append()` method adds an item to the end of the list.

4. What will be the output of the following code?

10
20
30
40

✅ 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?

clear()
delete()
removeAll()
empty()

✅ Correct Answer

The `clear()` method removes all elements from a list, leaving it empty.

6. What is the output of the following code?

[1, 2, 3]
[1, 2, 3, 4]
[4]
Error

✅ 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?

list[last]
list[-1]
list[len(list)]
list[0]

✅ Correct Answer

Negative indexing allows access from the end; `list[-1]` gives the last element.

8. What will the following code print?

[2, 3]
[1, 2]
[3, 4]
[1, 3]

✅ 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?

list1 + list2
list1.append(list2)
list1.extend(list2)
Both 1 and 3

✅ 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?

[20, 5, 10, 15]
[5, 20, 10, 15]
[5, 10, 20, 15]
[5, 10, 15, 20]

✅ 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?

10
20
30
40

✅ 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?

add()
insert()
append()
extend()

✅ 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?

[1, 2, 3, 4, 5]
[5, 7, 8]
Error
[[1, 2, 3], [4, 5]]

✅ Correct Answer

The `+` operator concatenates two lists in Python, not adds their elements.

14. What is the output of the following code?

[1, 3, 4]
[2, 3, 4]
[1, 2, 3]
Error

✅ 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?

sort()
order()
arrange()
ascending()

✅ Correct Answer

The `sort()` method sorts the list in place by default.

16. What will be printed?

True
False
None
Error

✅ 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?

[1, 2, 3]
[2, 3, 4]
[3, 4, 5]
[1, 2, 3, 4]

✅ 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?

list = list(1, 2, 3, 4, 5)
list = [1, 2, 3, 4, 5]
list = (1, 2, 3, 4, 5)
list = {1, 2, 3, 4, 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?

[1, 2, 3, 4, 5]
[1, 2, 3, [4, 5]]
[[1, 2, 3], [4, 5]]
Error

✅ 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?

`append()` adds elements individually, while `extend()` adds the whole list as one element.
`append()` adds one element, while `extend()` adds multiple elements from another iterable.
Both do the same thing.
`extend()` can only be used with strings.

✅ Correct Answer

`append()` adds its argument as a single item, whereas `extend()` iterates over its argument and adds each element to the list.

Score: 0 / 20