ProgrammingModerateTCS NQT · Wipro pattern

What is printed?

What is printed?
d = {'a': 1}
keys = d.keys()
d['b'] = 2
del d['a']
print(list(keys), len(keys))
  1. A. ['a', 'b'] 2
  2. B. ['b'] 1
  3. C. ['a'] 1
  4. D. ['b'] 2

Correct answer: B. ['b'] 1

keys() returns a live view that reflects later insertions and deletions. After the changes the only key left is b.

More Python Programming questions

Exams that ask this topic