ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
print(sorted({'b': 1, 'a': 1}.items()), list({3: 'x', 1: 'y'}), dict(zip('ab', [1, 2])), dict.fromkeys('ab', 0))
  1. A. [('b', 1), ('a', 1)] [1, 3] {'a': 1, 'b': 2} {'a': 0, 'b': 0}
  2. B. [('a', 1), ('b', 1)] [1, 3] {'a': 1, 'b': 2} {'a': 0, 'b': 0}
  3. C. [('a', 1), ('b', 1)] [3, 1] {'a': 2, 'b': 1} {'a': 0, 'b': 0}
  4. D. [('a', 1), ('b', 1)] [3, 1] {'a': 1, 'b': 2} {'a': 0, 'b': 0}

Correct answer: D. [('a', 1), ('b', 1)] [3, 1] {'a': 1, 'b': 2} {'a': 0, 'b': 0}

sorted on dict items orders the pairs by key, and a dict iterates over its keys in insertion order, so list gives 3 then 1. zip pairs a with 1 and b with 2, and fromkeys sets every key to 0.

More Python Programming questions

Exams that ask this topic