ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
d = {}
for w in ['b', 'a', 'b']:
d[w] = d.get(w, 0) + 1
print(d, max(d, key=d.get), sorted(d.values(), reverse=True))- A. {'b': 2, 'a': 1} a [1, 2]
- B. {'b': 2, 'a': 1} b [1, 2]
- C. {'b': 2, 'a': 1} b [2, 1]
- D. {'a': 1, 'b': 2} b [2, 1]
Correct answer: C. {'b': 2, 'a': 1} b [2, 1]
The dict keeps insertion order, so 'b' comes first. max with key=d.get returns the key with the largest count, which is b. The sorted values are 2 and 1 in descending order.
More Python Programming questions
- Which is the time complexity of checking whether a string s of length n is a palindrome, using s == s[::-1]?
- What is printed? python values = [0, 1, 2, 3] print([v for v in values if v] == values[1:], any(values), all(values[1:]))
- What is printed? python import heapq nums = [7, 2, 9] heapq.heapify(nums) print(nums[0], heapq.nlargest(1, nums)[0], heapq.nsmallest(2…
- What is printed? python print(any(x > 3 for x in range(10)), next(x for x in range(10) if x x > 20), sum(x for x in range(0, 10, 4)))
- What is printed? python s = '12:34:56' h, m, sec = map(int, s.split(':')) print(h 3600 + m 60 + sec, divmod(h 3600 + m 60 + sec, 60))
- What is printed? python print([x % 3 for x in [-7, -3, 0, 4]])