ProgrammingHardCognizant GenC · TCS NQT

What is printed?

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

Correct answer: C. {'x': 1, 'y': 3, 'z': 4} {'x': 9, 'y': 3, 'z': 4}

a | b makes a new dict in which later keys win, so c holds y = 3. update changes a in place, applies b first and the keyword x = 9 last.

More Python Programming questions

Exams that ask this topic