ProgrammingModerateAccenture · Capgemini pattern
What is printed?
What is printed?
words = ["banana", "Apple", "cherry", "apple"] print(sorted(words), sorted(words, key=str.lower)[:2], max(words, key=len))
- A. ['apple', 'Apple', 'banana', 'cherry'] ['Apple', 'apple'] banana
- B. ['Apple', 'apple', 'banana', 'cherry'] ['Apple', 'apple'] banana
- C. ['Apple', 'banana', 'cherry', 'apple'] ['Apple', 'apple'] banana
- D. ['Apple', 'apple', 'banana', 'cherry'] ['apple', 'Apple'] cherry
Correct answer: B. ['Apple', 'apple', 'banana', 'cherry'] ['Apple', 'apple'] banana
Default string order compares code points, and every uppercase letter comes before every lowercase one, so 'Apple' sorts first. With key=str.lower, 'Apple' and 'apple' tie and the stable sort keeps their original order. 'banana' and 'cherry' both have length 6; max returns the first maximum.
More Python Programming questions
- 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…
- What is printed? python it = iter([1, 2, 3, 4, 5]) pairs = list(zip(it, it)) print(pairs, list(it))
- What is printed? python import copy a = [[1, 2], [3]] b = copy.copy(a) c = copy.deepcopy(a) a[0].append(9) a.append([4]) print(b, c)