ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
a = {"x": 1, "y": 2}
b = {"y": 3, "z": 4}
c = a | b
a |= {"w": 0}
print(c, b | a)
  1. A. {'x': 1, 'y': 2, 'z': 4} {'y': 3, 'z': 4, 'x': 1, 'w': 0}
  2. B. {'x': 1, 'y': 3, 'z': 4} {'y': 3, 'z': 4, 'x': 1, 'w': 0}
  3. C. {'x': 1, 'y': 3, 'z': 4} {'x': 1, 'y': 2, 'z': 4, 'w': 0}
  4. D. {'x': 1, 'y': 3, 'z': 4} {'y': 2, 'z': 4, 'x': 1, 'w': 0}

Correct answer: D. {'x': 1, 'y': 3, 'z': 4} {'y': 2, 'z': 4, 'x': 1, 'w': 0}

In d1 | d2 the right operand's values win but key order starts with the left operand's keys. So b | a keeps b's order (y, z) then adds x, w, with y taking a's value 2.

More Python Programming questions

Exams that ask this topic