Python MCQ on Lists

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. Which of the following creates an empty list in Python?

a) list = ()
b) list = []
c) list = {}
d) list = list()

Answer:

b) list = []

Explanation:

In Python, an empty list can be created using empty square brackets [].

2. Which index does Python use to begin indexing an item in a list?

a) 1
b) -1
c) 0
d) Any positive integer

Answer:

c) 0

Explanation:

Python uses zero-based indexing. So, the first item in a list has index 0.

3. What will be the result of the following code?

my_list = [1, 2, 3, 4]
print(my_list[-1])
a) 1
b) 4
c) Error
d) None

Answer:

b) 4

Explanation:

Negative indexing starts from the end of the list. -1 denotes the last item.

4. Which method would you use to add an item to the end of a list?

a) insert()
b) append()
c) extend()
d) add()

Answer:

b) append()

Explanation:

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

5. How can you add multiple items to a list?

a) insert()
b) append()
c) extend()
d) add()

Answer:

c) extend()

Explanation:

The extend() method is used to add multiple items to a list.

6. What is the output of the following code?

fruits = ['apple', 'banana', 'cherry']
fruits.insert(1, 'orange')
print(fruits)
a) [‘apple’, ‘banana’, ‘orange’, ‘cherry’]
b) [‘apple’, ‘orange’, ‘banana’, ‘cherry’]
c) [‘orange’, ‘apple’, ‘banana’, ‘cherry’]
d) [‘apple’, ‘banana’, ‘cherry’, ‘orange’]

Answer:

b) [‘apple’, ‘orange’, ‘banana’, ‘cherry’]

Explanation:

The insert() method inserts an item at a specified index.

7. How can you remove the last item from a list?

a) remove()
b) delete()
c) pop()
d) discard()

Answer:

c) pop()

Explanation:

The pop() method removes the last item from a list if no index is specified.

8. Which method removes the first occurrence of an item in a list?

a) delete()
b) remove()
c) discard()
d) pop()

Answer:

b) remove()

Explanation:

The remove() method removes the first occurrence of the specified item from the list.

9. What will be the result of the following code?

numbers = [1, 2, 3, 4, 3, 5]
numbers.remove(3)
print(numbers)
a) [1, 2, 3, 4, 5]
b) [1, 2, 4, 3, 5]
c) [1, 2, 4, 5]
d) Error

Answer:

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

Explanation:

The remove() method removes the first occurrence of the specified item.

10. Which method returns the index of the first occurrence of an item?

a) index()
b) find()
c) locate()
d) search()

Answer:

a) index()

Explanation:

The index() method returns the index of the first occurrence of the specified item in the list.

11. What is the output of the following code?

my_list = [1, 2, 3]
another_list = my_list
another_list[0] = 4
print(my_list)
a) [1, 2, 3]
b) [4, 2, 3]
c) [4, 4, 4]
d) Error

Answer:

b) [4, 2, 3]

Explanation:

Lists are mutable, and when you assign a list to another variable (like another_list in the example), both variables point to the same list in memory. Any changes made using one variable are reflected in the other.

12. How can you create a copy of a list?

a) new_list = list.copy()
b) new_list = list
c) new_list = list[:]
d) Both a and c

Answer:

d) Both a and c

Explanation:

You can create a copy of a list either using the copy() method or by slicing the entire list.

13. What will be the result of the following code?

my_list = [10, [3.141, 20, [30, 'baz', 2.718]], 'foo']
print(my_list[1][2][1])
a) 20
b) ‘baz’
c) 2.718
d) 3.141

Answer:

b) ‘baz’

Explanation:

The code fetches the 2nd item (index 1) of the main list, then the 3rd item (index 2) of the nested list, and finally the 2nd item (index 1) of the innermost list.

14. What method would you use to count the occurrences of an item in a list?

a) count()
b) find()
c) occur()
d) number()

Answer:

a) count()

Explanation:

The count() method returns the number of occurrences of the specified item in the list.

15. Which of the following statements is correct?

a) Lists are immutable.
b) Lists can contain items of multiple data types.
c) Lists can only have integer items.
d) Lists cannot contain other lists.

Answer:

b) Lists can contain items of multiple data types.

Explanation:

Lists in Python are mutable and can contain items of any data type, including other lists.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top