ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
from collections import Counter
c = Counter()
c['a'] += 2
c.update('ab')
print(c['a'], c['b'], c['z'], sorted(c.elements()))- A. 3 1 0 ['a', 'a', 'b']
- B. 3 1 0 ['a', 'a', 'a', 'b']
- C. 2 1 0 ['a', 'a', 'a', 'b']
- D. 3 1 1 ['a', 'a', 'a', 'b']
Correct answer: B. 3 1 0 ['a', 'a', 'a', 'b']
Counter's missing keys read as zero, and += and update both add to the count. The count for a is 2 + 1 = 3, and elements() expands each count, which gives a three times.
More Python Programming questions
- What is printed? python print(pow(2, 1018, 109 + 7) == pow(2, 1018 % (109 + 6), 109 + 7))
- What is printed? python print(7 // 0.5, 7 % 0.5, 0.5 7 // 1, 1e308 10 == float('inf'))
- What is printed? python print(sum([True, True, False]), True 3, 'ab' True, [0] True)
- What is printed? python print('%x' % 255, '%X' % 255, '%#o' % 8, '%+d' % 0)
- What is printed? python print(float('1e-7'), 0.000001, 1e-6 == 0.000001, 123456789012345678.0)
- What is printed? python print(round(0.5 + 1), round(-0.5 - 1), round(2.5 - 0.0001))