ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
print(len([*range(3), *'ab']), [*'ab', *[1]], {**{'a': 1}, 'a': 2})
  1. A. 4 ['a', 'b', 1] {'a': 1}
  2. B. 5 ['a', 'b'] {'a': 2}
  3. C. 5 ['a', 'b', 1] {'a': 2}
  4. D. 5 ['a', 'b', 1] {'a': 1, 'a': 2}

Correct answer: C. 5 ['a', 'b', 1] {'a': 2}

Star unpacking in a list display adds every item, so the first list has three numbers and two letters. A later key in a dict display overrides an earlier one, so a becomes 2.

More Python Programming questions

Exams that ask this topic