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))- A. [('b', 1), ('a', 1)] [1, 3] {'a': 1, 'b': 2} {'a': 0, 'b': 0}
- B. [('a', 1), ('b', 1)] [1, 3] {'a': 1, 'b': 2} {'a': 0, 'b': 0}
- C. [('a', 1), ('b', 1)] [3, 1] {'a': 2, 'b': 1} {'a': 0, 'b': 0}
- 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
- What is printed? python print(len({1, 1.0, True, '1'}), len({'1', 1}), {1, 2} == {2.0, 1.0}, {0} == {False})
- What is printed? python print(float('1' + '0' 308) == 1e308, 10 308 > 1e308, 10 308 < 1e308)
- What is printed? python from functools import reduce print(reduce(lambda acc, ch: acc 10 + int(ch), '2024', 0), reduce(lambda acc, ch: acc…
- What is printed? python print('{0:>{1}}'.format('x', 3), '{:0>3}'.format(7), '{:x<4}'.format('ab'), '{:^7.3f}|'.format(2.5))
- What is printed? python print(1 == True == 1.0, 2 > 1 == True, (2 > 1) == True, 1 < 2 == True)
- What is printed? python print(int(' 0x1f ', 16), int('1e3', 16), float('1e3'))