Python list MCQ questions with answers
Slicing, list methods, copies and aliases: the list questions exams ask most.
0 of 20 answered
Question 1 · Easy
What is the output of this code?
nums = [10, 20, 30, 40, 50] print(nums[1:4], nums[-1])
Show the answer
Answer: A
The slice
Run this code in the editor[1:4]starts at index 1 and stops before index 4, so it holds 20, 30 and 40. Index-1counts from the end and gives the last item, 50.Question 2 · Easy
What is the output of this code?
a = [1, 2] b = [1, 2] a.append([3, 4]) b.extend([3, 4]) print(len(a), len(b))
Show the answer
Answer: B
Run this code in the editorappend()adds its argument as one item, soabecomes[1, 2, [3, 4]]with length 3.extend()adds each item of the list, sobbecomes[1, 2, 3, 4]with length 4.Question 3 · Easy
What is the output of this code?
letters = ['a', 'b', 'c'] letters.insert(1, 'x') print(letters)
Show the answer
Answer: C
Run this code in the editorinsert(1, 'x')puts'x'at index 1 and shifts the later items one place to the right. Nothing is replaced, so the list grows to four items.Question 4 · Easy
What is the output of this code?
scores = [4, 8, 15, 16, 23, 42] print(len(scores), max(scores) - min(scores), 15 in scores)
Show the answer
Answer: D
The list has 6 items.
Run this code in the editormax()is 42 andmin()is 4, so the difference is 38.inchecks whether 15 is one of the items, which it is.Question 5 · Easy
What is the output of this code?
a = [1, 2, 3] b = a b.append(4) print(a)
Show the answer
Answer: B
Run this code in the editorb = adoes not copy the list: both names refer to the same list object. Appending throughbchanges the list thatashows too. Usea.copy()ora[:]for a separate list.Question 6 · Easy
Which statement about
list.sort()andsorted()is true?Show the answer
Answer: D,
sort()changes the list in place and returnsNone;sorted()returns a new listsort()is a list method that reorders the list itself and returnsNone.sorted()is a built-in function that accepts any iterable (a tuple, a string, a dictionary) and returns a new list.Question 7 · Easy
What happens when this code runs?
nums = [10, 20, 30] del nums[1] print(nums[2])
Show the answer
Answer: B, IndexError: list index out of range
Run this code in the editordel nums[1]removes 20, so the list is[10, 30]and its valid indexes are 0 and 1. Reading index 2 raisesIndexError.KeyErroris what a dictionary raises for a missing key.Question 8 · Easy
Which line changes
nums = [1, 2, 3]into[1, 2, 3, 4, 5]?Show the answer
Answer: B
extend()adds each item of its argument to the end.append()andinsert()add the whole list as one nested item, and assigning the result ofextend()storesNone, because it returns nothing.Question 9 · Medium
What is the output of this code?
marks = [72, 45, 90] result = marks.sort() print(result, marks)
Show the answer
Answer: C
Run this code in the editorsort()sorts the list in place and returnsNone, soresultisNonewhilemarksitself is now sorted. Usesorted(marks)when you want a new sorted list back.Question 10 · Medium
What is the output of this code?
grid = [[0] * 3] * 3 grid[0][0] = 5 print(grid)
Show the answer
Answer: D
Run this code in the editor[[0] * 3] * 3repeats a reference to one inner list three times, so all three rows are the same object and a change togrid[0][0]shows in every row.[[0] * 3 for _ in range(3)]builds three separate rows.Question 11 · Medium
What is the output of this code?
nums = [5, 3, 5, 7, 3] nums.remove(3) x = nums.pop(1) print(x, nums)
Show the answer
Answer: A
Run this code in the editorremove(3)deletes only the first 3, leaving[5, 5, 7, 3].pop(1)removes and returns the item at index 1, which is 5, leaving[5, 7, 3].remove()takes a value;pop()takes an index.Question 12 · Medium
What is the output of this code?
nums = [0, 1, 2, 3, 4, 5, 6] print(nums[5:1:-2])
Show the answer
Answer: B
With a step of -2 the slice starts at index 5 and moves left two places at a time: 5, then 3. It stops before index 1, so 1 is not included.
Run this code in the editorQuestion 13 · Medium
After
a = [1, 2, 3]andb = [1, 2, 3], which statement is true?Show the answer
Answer: A,
a == bis True anda is bis False==compares values, and the two lists hold the same items.ischecks whether both names refer to the same object, and each list display creates a new list.Question 14 · Medium
What happens when this code runs?
names = ['Asha', 'Ben'] names = names.append('Chen') names.append('Dev')
Show the answer
Answer: C, AttributeError: 'NoneType' object has no attribute 'append'
Run this code in the editorappend()changes the list in place and returnsNone, so the second line setsnamestoNone. The next call then fails becauseNonehas noappendmethod.Question 15 · Medium
Which error does this code raise?
items = [3, 'two', 1] items.sort() print(items)
Show the answer
Answer: A, TypeError: '<' not supported between instances of 'str' and 'int'
Sorting compares items with
Run this code in the editor<, and Python 3 does not order a string against a number, sosort()raisesTypeError. Python never converts'two'to a number for you.Question 16 · Medium
Which expression returns a reversed copy of
numsand leavesnumsunchanged?Show the answer
Answer: C
A slice with step -1 walks the list backwards and builds a new list.
reverse()changes the list in place and returnsNone,sorted(..., reverse=True)sorts in descending order (not the same as reversing), andnums[-1:]is only the last item.Question 17 · Hard
What is the output of this code?
a = [1, 2] b = a a += [3] a = a + [4] print(b)
Show the answer
Answer: C
Run this code in the editora += [3]extends the list in place, sob, which names the same list, sees[1, 2, 3].a = a + [4]builds a new list and pointsaat it;bstill holds the old one.Question 18 · Hard
What is the output of this code?
original = [[1, 2], [3, 4]] clone = original.copy() clone[0].append(99) clone[1] = [0] print(original)
Show the answer
Answer: D
Run this code in the editorcopy()is shallow: a new outer list holding the same inner lists. Appending toclone[0]changes an inner list both share, butclone[1] = [0]only replaces a slot in the new outer list.copy.deepcopy()copies the inner lists too.Question 19 · Hard
What is the output of this code?
nums = [1, 2, 2, 3, 2] for n in nums: if n == 2: nums.remove(n) print(nums)
Show the answer
Answer: A
Removing items from a list while looping over it makes the loop skip items: after each removal the next value slides into an index the loop has already passed, so one 2 is never checked. Loop over a copy (
Run this code in the editornums[:]) or build a new list instead.Question 20 · Hard
What does
a[1:2] = [7, 8, 9]do whena = [1, 2, 3]?Show the answer
Answer: D, Replaces 2 with three items:
abecomes[1, 7, 8, 9, 3]Assigning to a slice replaces that part of the list with the items on the right, and the lengths need not match, so the list grows. Only slices with a step, such as
a[::2], need the same number of items.
Keep going
- Read the lesson behind these questions: Lists in the PythonBook
- Next topic: Python tuple MCQ questions with answers
- Before this: Python loops MCQ questions with answers
- Write the code yourself in the Python coding challenges, which check your solution with tests.