ProgrammingModerateTCS NQT · Accenture
What is printed?
What is printed?
nums = [10, 9, 100, 1] print(sorted(nums, key=str), max(nums, key=str), sorted(map(str, nums))[-1])
- A. [1, 9, 10, 100] 100 9
- B. [1, 10, 100, 9] 9 9
- C. [1, 10, 100, 9] 100 9
- D. [1, 10, 100, 9] 9 100
Correct answer: B. [1, 10, 100, 9] 9 9
Sorting with key=str compares decimal strings, so '1' < '10' < '100' < '9'. The largest string is '9', which gives 9 for max and for the last sorted string.
More Python Programming questions
- What is printed? python def ngecirc(a): n = len(a) res = [-1] n st = [] for i in range(2 n): x = a[i % n] while st and a[st[-1]] < x…
- What is printed? python def dsum(n): return n if n < 10 else dsum(sum(map(int, str(n)))) print(dsum(9875), dsum(0), dsum(999999999999))
- What is printed? python def lcp(words): out = [] for col in zip(words): if len(set(col)) == 1: out.append(col[0]) else: break return…
- What is printed? python s = 'abc' 2 + 'd' print(s[::3], 'bcd' in s, s.index('c', 3))
- What is printed? python print(range(0, 10, 3) == range(0, 11, 3), range(0, 10, 3)[-1], len(range(1018)))
- What is printed? python s = 'abc' t = s s += 'd' u = list('abc') v = u u += ['d'] print(s, t, u is v, v)