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])- A. {1: 'x', 2: 'y'} {'a': 1, 'b': 2} q
- B. {1: 'x', 2: 'y'} {'a': 1, 'b': 2, 'c': None} q
- C. {'x': 1, 'y': 2} {'a': 1, 'b': 2} p
- 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
- What is printed? python a = b = [] a.append(1) c, d = [], [] c.append(1) x = y = 5 x += 1 print(b, d, y)
- What is printed? python a = [1, 2, 3] i = 0 i, a[i] = 2, 9 print(a, i)
- What is printed? python x = [0, 1] i = 0 x[i], i = 5, 1 j = 1 j, x[j] = 0, 7 print(x, i, j)
- What is printed? python a, b = 1, 2 a, b = b, a + b c = [3, 4] c[0], c[1] = c[1], c[0] m, n = n, m = 7, 8 print(a, b, c, m, n)
- What is printed? python b = "Hé".encode("utf-8") print(len("Hé"), len(b), b[1], b.decode("latin-1"))
- What is printed? python b = b"abc" print(b[0], b[:1], list(b"hi"), bytes([65, 66]), b"a" + bytearray(b"b"))