Editor

Python list MCQ questions with answers

Slicing, list methods, copies and aliases: the list questions exams ask most.

0 of 20 answered

  1. 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 [1:4] starts at index 1 and stops before index 4, so it holds 20, 30 and 40. Index -1 counts from the end and gives the last item, 50.

    Run this code in the editor
  2. 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

    append() adds its argument as one item, so a becomes [1, 2, [3, 4]] with length 3. extend() adds each item of the list, so b becomes [1, 2, 3, 4] with length 4.

    Run this code in the editor
  3. 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

    insert(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.

    Run this code in the editor
  4. 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. max() is 42 and min() is 4, so the difference is 38. in checks whether 15 is one of the items, which it is.

    Run this code in the editor
  5. 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

    b = a does not copy the list: both names refer to the same list object. Appending through b changes the list that a shows too. Use a.copy() or a[:] for a separate list.

    Run this code in the editor
  6. Question 6 · Easy

    Which statement about list.sort() and sorted() is true?

    Show the answer

    Answer: D, sort() changes the list in place and returns None; sorted() returns a new list

    sort() is a list method that reorders the list itself and returns None. sorted() is a built-in function that accepts any iterable (a tuple, a string, a dictionary) and returns a new list.

  7. 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

    del nums[1] removes 20, so the list is [10, 30] and its valid indexes are 0 and 1. Reading index 2 raises IndexError. KeyError is what a dictionary raises for a missing key.

    Run this code in the editor
  8. 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() and insert() add the whole list as one nested item, and assigning the result of extend() stores None, because it returns nothing.

  9. 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

    sort() sorts the list in place and returns None, so result is None while marks itself is now sorted. Use sorted(marks) when you want a new sorted list back.

    Run this code in the editor
  10. 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

    [[0] * 3] * 3 repeats a reference to one inner list three times, so all three rows are the same object and a change to grid[0][0] shows in every row. [[0] * 3 for _ in range(3)] builds three separate rows.

    Run this code in the editor
  11. 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

    remove(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.

    Run this code in the editor
  12. 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 editor
  13. Question 13 · Medium

    After a = [1, 2, 3] and b = [1, 2, 3], which statement is true?

    Show the answer

    Answer: A, a == b is True and a is b is False

    == compares values, and the two lists hold the same items. is checks whether both names refer to the same object, and each list display creates a new list.

  14. 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'

    append() changes the list in place and returns None, so the second line sets names to None. The next call then fails because None has no append method.

    Run this code in the editor
  15. 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 <, and Python 3 does not order a string against a number, so sort() raises TypeError. Python never converts 'two' to a number for you.

    Run this code in the editor
  16. Question 16 · Medium

    Which expression returns a reversed copy of nums and leaves nums unchanged?

    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 returns None, sorted(..., reverse=True) sorts in descending order (not the same as reversing), and nums[-1:] is only the last item.

  17. 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

    a += [3] extends the list in place, so b, which names the same list, sees [1, 2, 3]. a = a + [4] builds a new list and points a at it; b still holds the old one.

    Run this code in the editor
  18. 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

    copy() is shallow: a new outer list holding the same inner lists. Appending to clone[0] changes an inner list both share, but clone[1] = [0] only replaces a slot in the new outer list. copy.deepcopy() copies the inner lists too.

    Run this code in the editor
  19. 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 (nums[:]) or build a new list instead.

    Run this code in the editor
  20. Question 20 · Hard

    What does a[1:2] = [7, 8, 9] do when a = [1, 2, 3]?

    Show the answer

    Answer: D, Replaces 2 with three items: a becomes [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