ProgrammingVery hardInfosys HackWithInfy · TCS CodeVita

What is printed?

What is printed?
d = {}
d.setdefault('k', []).append(1)
d.setdefault('k', []).append(2)
print(d, d.setdefault('j', 9), 'j' in d)
  1. A. {'k': [1, 2]} 9 True
  2. B. {'k': [1, 2], 'j': 9} 9 False
  3. C. {'k': [1], 'j': 9} 9 True
  4. D. {'k': [1, 2], 'j': 9} 9 True

Correct answer: D. {'k': [1, 2], 'j': 9} 9 True

print receives the same dict object, and setdefault('j', 9) inserts j before the output is formatted. The dict therefore shows j, and 'j' in d is True.

More Python Programming questions

Exams that ask this topic