ProgrammingModerateGATE CS · Product-company placement

What is printed?

What is printed?
a = {"x": 1, "y": 2}
b = {v: k for k, v in a.items()}
c = {k: v for k, v in zip("abc", [1, 2])}
print(b, c, {1: "p", 1.0: "q"}[True])
  1. A. {1: 'x', 2: 'y'} {'a': 1, 'b': 2} q
  2. B. {1: 'x', 2: 'y'} {'a': 1, 'b': 2, 'c': None} q
  3. C. {'x': 1, 'y': 2} {'a': 1, 'b': 2} p
  4. D. {1: 'x', 2: 'y'} {'a': 1, 'b': 2} KeyError

Correct answer: A. {1: 'x', 2: 'y'} {'a': 1, 'b': 2} q

The comprehension inverts the mapping. zip stops at the shorter input, so 'c' never appears. 1 and 1.0 (and True) are the same dict key, so the later value 'q' is stored and True finds it.

More Python Programming questions

Exams that ask this topic