ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
w = ["bob", "Al", "al", "Bo"] print(sorted(w), sorted(w, key=str.lower))
- A. ['al', 'Al', 'bob', 'Bo'] ['Al', 'al', 'Bo', 'bob']
- B. ['Al', 'Bo', 'al', 'bob'] ['al', 'Al', 'bob', 'Bo']
- C. ['Al', 'al', 'Bo', 'bob'] ['Al', 'al', 'Bo', 'bob']
- D. ['Al', 'Bo', 'al', 'bob'] ['Al', 'al', 'Bo', 'bob']
Correct answer: D. ['Al', 'Bo', 'al', 'bob'] ['Al', 'al', 'Bo', 'bob']
Default string order is by code point, so every capitalised word precedes every lowercase one. With key=str.lower, 'Al' and 'al' tie and the stable sort keeps their original order; 'bo' < 'bob' as a prefix.
More Python Programming questions
- What is printed? python people = [("ann", 30), ("bob", 25), ("cid", 30), ("dan", 25)] people.sort(key=lambda p: p[0], reverse=True)…
- What is printed? python nums = [4, -1, 3, -1, 2] print(sorted(nums, key=lambda v: (v < 0, -v)), sorted(set(nums), reverse=True)[:2])
- What is printed? python def walk(d, depth=0): out = [] for k, v in d.items(): out.append(f"{k}{depth}") if isinstance(v, dict): out +=…
- What is printed? python def hanoi(n): return 0 if n == 0 else 2 hanoi(n - 1) + 1 def calls(n): return 1 if n == 0 else 1 + 2 calls(n - 1)…
- What is printed? python import sys def depth(n): try: return depth(n + 1) except RecursionError: return n d = depth(0) print(d > 500, d <…
- What is printed? python x = 0 def a(): global x x = 1 def b(): global x x += 10 b() return x print(a(), x)