QuizLander
← All public quizzes

Python — Data structures (list, tuple, dict, set)

by Jude · 23/05/2026 · 124 views · 0 shares

Built-in collections: methods, mutability, complexity and classic gotchas. 20 questions.
20
Questions
🇬🇧 English
Language
Medium
Difficulty
Start the quiz (as a guest or with an account)
Share as image

Questions in this quiz

  1. 1. What is l after this?
    l = [1, 2, 3]
    l.append([4, 5])
    print(l)
    1. [1, 2, 3, 4, 5]
    2. [1, 2, 3, [4, 5]]
    3. [[1, 2, 3], [4, 5]]
    4. Error
  2. 2. Which built-in is immutable?
    1. list
    2. dict
    3. set
    4. tuple
  3. 3. Average complexity of x in d for a dict?
    1. O(1)
    2. O(log n)
    3. O(n)
    4. O(n log n)
  4. 4. What is printed?
    d = {'a': 1, 'b': 2}
    d.setdefault('a', 99)
    d.setdefault('c', 3)
    print(d)
    1. {'a': 1, 'b': 2, 'c': 3}
    2. {'a': 99, 'b': 2, 'c': 3}
    3. {'a': 1, 'b': 2}
    4. KeyError
  5. 5. How do you de-duplicate while preserving order?
    1. set(lst)
    2. list(dict.fromkeys(lst))
    3. sorted(set(lst))
    4. tuple(lst)
  6. 6. What is printed?
    a = [1, 2, 3]
    b = a
    b.append(4)
    print(a)
    1. [1, 2, 3]
    2. [1, 2, 3, 4]
    3. [4, 1, 2, 3]
    4. Error
  7. 7. Which creates the set {1, 2, 3}?
    1. {}
    2. set()
    3. {1, 2, 3}
    4. [1, 2, 3]
  8. 8. Given t = (10, 20, 30, 40), what is t[1:3]?
    1. (10, 20)
    2. (20, 30)
    3. (20, 30, 40)
    4. [20, 30]
  9. 9. Which op is not allowed on a set?
    1. s.add(1)
    2. s | {2}
    3. s[0]
    4. s.discard(1)
  10. 10. What does this print?
    nums = [3, 1, 4, 1, 5, 9, 2]
    print(sorted(nums, reverse=True)[:3])
    1. [1, 2, 3]
    2. [9, 5, 4]
    3. [3, 1, 4]
    4. [9, 5, 4, 3]
  11. 11. What does this print?
    m = [[0]*3]*3
    m[0][0] = 9
    print(m)
    1. [[9, 0, 0], [0, 0, 0], [0, 0, 0]]
    2. [[9, 0, 0], [9, 0, 0], [9, 0, 0]]
    3. [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
    4. Error
  12. 12. Which removes the last element in amortized O(1)?
    1. lst.pop(0)
    2. lst.pop()
    3. del lst[len(lst)//2]
    4. lst.remove(lst[-1])
  13. 13. Best structure for O(1) FIFO at both ends?
    1. list
    2. collections.deque
    3. set
    4. tuple
  14. 14. What does this print?
    from collections import Counter
    c = Counter("mississippi")
    print(c.most_common(2))
    1. [('i', 4), ('s', 4)]
    2. [('s', 4), ('i', 4)]
    3. [('m', 1), ('p', 2)]
    4. Error
  15. 15. What does frozenset({1,2}) in {frozenset({1,2}): 'x'} return?
    1. True
    2. False
    3. TypeError
    4. None
  16. 16. What is the output?
    d = {'a': 1, 'b': 2}
    for k, v in sorted(d.items(), key=lambda kv: -kv[1]):
        print(k, v)
    1. a 1 then b 2
    2. b 2 then a 1
    3. a 2 then b 1
    4. Error
  17. 17. What does this print?
    lst = [1, 2, 3, 4, 5]
    print(lst[::-2])
    1. [5, 3, 1]
    2. [1, 3, 5]
    3. [5, 4, 3]
    4. [4, 2]
  18. 18. Which value is valid as a dict key?
    1. A list
    2. A set
    3. A tuple of hashable items
    4. A dict
  19. 19. Difference between {} and set()?
    1. None
    2. {} is an empty dict; set() is an empty set
    3. {} is immutable
    4. set() uses less memory
  20. 20. What is printed?
    d = {'x': 1, 'y': 2, 'z': 3}
    view = d.keys()
    del d['x']
    print(list(view))
    1. ['x', 'y', 'z']
    2. ['y', 'z']
    3. ['x']
    4. RuntimeError
Answer the questions and see my score

Comments

Sign in to comment.

Loading…