ProgrammingHardTCS NQT · Infosys
What does this Python code print? words = ["bb", "a", "ccc", "dd", "e", "fff"] print(sorted(words, key=len, reverse=True)[:3], min(words, key=len), max(words, key=len))
- A. ['ccc', 'fff', 'bb'] a ccc
- B. ['fff', 'ccc', 'dd'] e fff
- C. ['ccc', 'fff', 'bb'] e ccc
- D. ['ccc', 'fff', 'bb'] a fff
Correct answer: A. ['ccc', 'fff', 'bb'] a ccc
sorted with reverse=True is still stable, so equal-length words keep their original order: ccc, fff, bb. min and max also return the first element among ties, giving 'a' and 'ccc'.
More Python Programming questions
- What is printed? python def f(): for i in range(3): try: if i == 1: continue if i == 2: return 'ret' finally: print('f', i, end=' ')…
- What is printed? python class A: def f(self): return 'A' class B(A): def f(self): return 'B' + super().f() class C(A): def f(self): return…
- What is printed? python def gen(): x = yield 1 print('got', x) y = yield x 2 return y g = gen() print(next(g)) print(g.send(10)) try…
- What is printed? python funcs = [] for i in range(3): funcs.append(lambda x=i: x 10) funcs.append(lambda: i 10) print([f() for f in funcs])
- What is printed? python class Tag: items = [] n = 0 def add(self, v): self.items += [v] self.n += 1 a, b = Tag(), Tag() a.add('x')…
- What is printed? python x = 10 class K: x = 20 ys = [x + i for i in range(2)] print(K.ys)