ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
print({'a': 1} | {'a': 2, 'b': 3}, {'a': 1} | {}, dict(a=1) == {'a': 1})
  1. A. {'a': 1, 'b': 3} {'a': 1} True
  2. B. {'a': 2, 'b': 3} {'a': 2} True
  3. C. {'a': 2, 'b': 3} {'a': 1} True
  4. D. {'a': 2, 'b': 3} {'a': 1} False

Correct answer: C. {'a': 2, 'b': 3} {'a': 1} True

The | operator merges dicts, and values from the right operand win when keys clash. Merging with an empty dict leaves the left dict unchanged, and dict(a=1) equals the literal.

More Python Programming questions

Exams that ask this topic