Python MCQs - Dictionaries, Sets and Comprehensions (Set 3)

Python MCQs - Dictionaries, Sets and Comprehensions (Set 3) - CodingHumans

This set covers Python's hash-based collections and the comprehension syntax used to build them. You will meet dictionary methods such as get, setdefault and fromkeys, the dict merge operator, set algebra, hashability rules for keys, Counter, and list, set, dict and generator comprehensions including nested loops and filters. These ideas are tested heavily in placement coding rounds, Python interviews and semester practicals, often through tricky output-prediction snippets like the ones below.

How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 18 questions.

Python MCQ series: Set 1 | Set 2 | Set 3 | Set 4 | Set 5 | Set 6

Topics covered in this set

  • Dictionary access with get and setdefault
  • dict.fromkeys and shared mutable values
  • Merging dictionaries with |
  • Set operations and subsets
  • Hashability and valid keys
  • List, set and dict comprehensions
  • Generator expressions and collections.Counter

Python MCQs with answers and explanations

Q1. What is the output of the following code?

d = {'a': 1, 'b': 2}
d['c'] = d.get('a', 0) + d.get('z', 10)
print(d['c'])

a) 11

b) 1

c) KeyError

d) 10

Q2. What is the output of the following code?

print({1, 2, 3} ^ {2, 3, 4}, {1, 2} <= {1, 2, 3})

a) {2, 3} True

b) {1, 4} False

c) {1, 2, 3, 4} True

d) {1, 4} True

Q3. What is the output of the following code?

d = {1: 'a', True: 'b', 1.0: 'c'}
print(d)

a) {1: 'c'}

b) {1: 'a', True: 'b', 1.0: 'c'}

c) {True: 'c'}

d) {1.0: 'c'}

Q4. What is the output of the following code?

print({x % 3 for x in range(10)})

a) [0, 1, 2, 0, 1, 2, 0, 1, 2, 0]

b) {0, 1, 2, 3}

c) {1, 2}

d) {0, 1, 2}

Q5. What is the output of the following code?

print({k: v for k, v in zip("abc", range(5))})

a) {'a': 0, 'b': 1, 'c': 2}

b) ValueError

c) {'a': 0, 'b': 1, 'c': 2, 3: 3, 4: 4}

d) {0: 'a', 1: 'b', 2: 'c'}

Q6. What is the output of the following code?

d = {[1, 2]: 'x'}
print(d)

a) TypeError

b) {[1, 2]: 'x'}

c) KeyError

d) {(1, 2): 'x'}

Q7. What is the output of the following code?

a = {}
b = set()
print(type(a).__name__, type(b).__name__)

a) set set

b) dict set

c) dict dict

d) set dict

Q8. What is the output of the following code?

d = {'x': 1}
print(d.setdefault('x', 5), d.setdefault('y', 7), d)

a) 5 7 {'x': 5, 'y': 7}

b) 1 7 {'x': 1, 'y': 7}

c) 1 7 {'x': 1}

d) 1 None {'x': 1, 'y': 7}

Q9. What is the output of the following code?

print([i * j for i in range(1, 3) for j in range(i, 3)])

a) [1, 2, 2, 4]

b) [1, 4]

c) [2, 4]

d) [1, 2, 4]

Q10. What is the output of the following code?

d = dict.fromkeys(['a', 'b'], [])
d['a'].append(1)
print(d)

a) {'a': [1], 'b': [1]}

b) {'a': [1], 'b': []}

c) {'a': 1, 'b': []}

d) TypeError

Q11. What is the output of the following code?

d = {1: 1, 2: 2}
for k in d:
    d[k + 10] = 0
print(d)

a) {1: 1, 2: 2, 11: 0, 12: 0}

b) {1: 1, 2: 2, 11: 0}

c) RuntimeError

d) KeyError

Q12. What is the output of the following code?

print([x for x in range(10) if x % 2 if x % 3])

a) [1, 3, 5, 7, 9]

b) [1, 5, 7]

c) [1, 5, 7, 9]

d) SyntaxError

Q13. What is the output of the following code?

g = (x * x for x in range(4))
print(sum(g), sum(g))

a) 14 14

b) 0 14

c) TypeError

d) 14 0

Q14. What is the output of the following code?

d1 = {'a': 1, 'b': 2}
d2 = {'b': 3, 'c': 4}
print(d1 | d2)

a) {'a': 1, 'b': 2, 'c': 4}

b) {'a': 1, 'b': 3, 'c': 4}

c) TypeError

d) {'b': 3}

Q15. What is the average-case time complexity of checking x in s when s is a Python set of n elements?

a) O(log n)

b) O(1)

c) O(n)

d) O(n log n)

Q16. What is the output of the following code?

x = 'outer'
squares = [x for x in range(3)]
print(x)

a) 2

b) 0

c) outer

d) NameError

Q17. Which of the following can be used as an element of a Python set?

a) {1, 2}

b) [1, 2]

c) frozenset({1, 2})

d) {'a': 1}

Q18. What is the output of the following code?

from collections import Counter
c = Counter("mississippi")
print(c.most_common(2))

a) [('s', 4), ('i', 4)]

b) [('i', 4), ('p', 2)]

c) [('i', 4), ('s', 4)]

d) [('m', 1), ('i', 4)]

Quick revision notes

  • Dictionary keys and set elements must be hashable (immutable built-ins, frozenset, tuples of hashables).
  • Keys that compare equal and hash equally (1, 1.0, True) are treated as the same key.
  • Since Python 3.7, dicts preserve insertion order as a language guarantee.
  • {} creates an empty dict; use set() for an empty set.
  • d.get(k, default) never raises KeyError; d.setdefault(k, v) also inserts v when k is missing.
  • d1 | d2 (Python 3.9+) merges dicts, with values from d2 winning on clashes.
  • Set and dict membership tests are O(1) on average; list membership is O(n).
  • A generator expression can be consumed only once.

Frequently asked questions

Why can't a list be used as a dictionary key in Python?

Dictionary keys must be hashable, and a list is mutable, so its hash could change after insertion and break the lookup table. Convert it to a tuple if you need a sequence as a key.

What is the difference between a list comprehension and a generator expression?

A list comprehension builds the whole list in memory immediately, while a generator expression produces values lazily one at a time. Generators save memory for large data but can only be iterated once.

Are Python dictionaries ordered?

Yes. From Python 3.7 onwards, dictionaries are guaranteed to preserve the order in which keys were inserted. Updating an existing key's value does not change its position.

Comments

Answered 0/0Correct 0