ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
print(len([*range(3), *'ab']), [*'ab', *[1]], {**{'a': 1}, 'a': 2})- A. 4 ['a', 'b', 1] {'a': 1}
- B. 5 ['a', 'b'] {'a': 2}
- C. 5 ['a', 'b', 1] {'a': 2}
- 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
- What is printed? python print(hash(()) == hash(()), (1, 2) < (1, 2, 0), 'b' 2 > 'ab' 2, (1, 'b') < (1, 'a'))
- What is printed? python print(sorted(['b', 'B', 'a'], key=lambda s: (s.lower(), s)))
- What is printed? python from math import prod print(prod([2, 3, 4]), prod([]), prod([0.5, 4]), prod(range(1, 4), start=10))
- What is printed? python print(2 -1 2 1, 2 -(1 2) 1, (2 -1) (2 1))
- What is printed? python print('%-5s|%5s|' % ('ab', 'cd'), '%(x)d-%(y)s' % {'x': 1, 'y': 'z'})
- What is printed? python print(0.1 3 == 0.30000000000000004, 0.1 3 - 0.3 == 5.551115123125783e-17, 0.3 - 0.1 3 == -5.551115123125783e-17)