Python Lists Quiz - MCQ Questions and Answers

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?

a) A collection of unique values
b) A collection of unordered, changeable, and indexed elements
c) A collection of ordered, changeable, and indexed elements
d) A fixed-size array

Answer:

c) A collection of ordered, changeable, and indexed elements

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?

a) Using curly braces {}
b) Using parentheses ()
c) Using square brackets []
d) Using angle brackets <>

Answer:

c) Using square brackets []

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?

a) list.add(element)
b) list.append(element)
c) list.insert(element)
d) list.set(element)

Answer:

b) list.append(element)

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

a) myList[2]
b) myList[3]
c) myList(2)
d) myList(3)

Answer:

a) myList[2]

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]

a) Elements from index 1 to 3
b) Elements from index 1 to 4
c) Elements from index 2 to 4
d) All elements of the list

Answer:

a) Elements from index 1 to 3

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?

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

Answer:

a) pop()

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

a) myList(0) = 'new_value'
b) myList[0] = 'new_value'
c) myList.set(0, 'new_value')
d) myList.replace(0, 'new_value')

Answer:

b) myList[0] = 'new_value'

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])?

a) 2
b) 3
c) 30
d) 60

Answer:

b) 3

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?

a) list1 + list2
b) list1.append(list2)
c) list1.concat(list2)
d) list1.extend(list2)

Answer:

a) list1 + list2

Explanation:

The + operator is used to concatenate two lists.

10. What does myList.insert(2, 'new_element') do?

a) Replaces the element at index 2 with 'new_element'
b) Adds 'new_element' to the list at index 2
c) Inserts 'new_element' after the element at index 2
d) Deletes the element at index 2 and adds 'new_element'

Answer:

b) Adds 'new_element' to the list at index 2

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?

a) Only integers
b) Only strings
c) Any type
d) Only integers and strings

Answer:

c) Any type

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

a) [1, 2, 3, 1, 2, 3, 1, 2, 3]
b) [3, 6, 9]
c) [1, 2, 3]
d) Error

Answer:

a) [1, 2, 3, 1, 2, 3, 1, 2, 3]

Explanation:

Multiplying a list by an integer n replicates its elements n times.

13. What does the remove() method do in a list?

a) Removes the first occurrence of a specified element
b) Removes the last element of the list
c) Removes all elements from the list
d) Removes an element at a specific index

Answer:

a) Removes the first occurrence of a specified element

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)
a) [1, 2, 3]
b) [4, 5]
c) [1, 2, 3, 4, 5]
d) Error

Answer:

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

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?

a) By placing one list inside another
b) By using the nest() method
c) By separating lists with a comma
d) By using the append() method

Answer:

a) By placing one list inside another

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)
a) [1, 2]
b) [1, 3]
c) [2, 3]
d) Error

Answer:

b) [1, 3]

Explanation:

The del statement removes the element at the specified index from the list.

17. What does list slicing myList[::2] return?

a) Every second element starting from the first
b) Every second element starting from the second
c) The entire list in reverse order
d) A copy of the original list

Answer:

a) Every second element starting from the first

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?

a) list.reverse()
b) reversed(list)
c) list.sort(reverse=True)
d) list.invert()

Answer:

a) list.reverse()

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

a) copyList = myList
b) copyList = myList.copy()
c) copyList = list(myList)
d) Both b) and c)

Answer:

d) Both b) and c)

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])
a) a
b) b
c) c
d) d

Answer:

d) d

Explanation:

Negative indices count from the end of the list, so myList[-1] refers to the last element 'd'.

Comments