ProgrammingModerateTCS NQT · Infosys pattern
What is printed?
What is printed?
from collections import Counter
c = Counter("mississippi")
print(c.most_common(2), c["z"], sum(c.values()))- A. [('s', 4), ('i', 4)] 0 11
- B. [('i', 4), ('s', 4)] KeyError 11
- C. [('i', 4), ('p', 2)] 0 11
- D. [('i', 4), ('s', 4)] 0 11
Correct answer: D. [('i', 4), ('s', 4)] 0 11
i and s both occur 4 times; most_common breaks ties by first-insertion order, and 'i' is seen before 's'. A missing key in a Counter returns 0 instead of raising KeyError. The counts add up to the string length, 11.
More Python Programming questions
- What is printed? python a, b, c = range(6) x, y = y, x = 1, 2 print(a, b, c, x, y)
- What is printed? python words = ["banana", "Apple", "cherry", "apple"] print(sorted(words), sorted(words, key=str.lower)[:2], max(words…
- What is printed? python i = 10 squares = [i i for i in range(3)] for j in range(3): pass print(i, squares, j)
- What is printed? python s = " Hello, World " print(s.strip().lower().split(","), s.count("l"), s.find("z"), "-".join("abc"))
- What is printed? python def f(args, kw): return len(args), sorted(kw) print(f(1, 2, [3, 4], a=5, {"b": 6}))
- What is printed? python class A: count = 0 def init(self): A.count += 1 self.count = 10 a = A(); b = A() b.count += 5 print(A.count…