Question 11 Medium What is the output of the following Python program? main.py Copy from collections import namedtuple Point = namedtuple("Point", "x y") p = Point(3, 4) q = p._replace(x=10) print(p, p.x * p.y, q, p == (3, 4)) A Point(x=3, y=4) 12 Point(x=10, y=4) True B Point(x=10, y=4) 12 Point(x=10, y=4) True C (3, 4) 12 (10, 4) True D Point(x=3, y=4) 12 Point(x=10, y=4) False View Answer & Explanation Correct Answer: Option A _replace returns a new namedtuple; p is unchanged. A namedtuple is still a tuple, so it equals (3, 4). Workspace Report Question page & discussion Report an error in this question Submit Report
Question 12 Medium What is the output of the following Python program? main.py Copy print(tuple("abc"), tuple([1, 2]), tuple({3: "a", 4: "b"}), tuple(range(3))) A ('abc',) (1, 2) (3, 4) (0, 1, 2) B ('a', 'b', 'c') (1, 2) (3, 4) (0, 1, 2) C ('a', 'b', 'c') (1, 2) ('a', 'b') (0, 1, 2) D ('a', 'b', 'c') ([1, 2],) ((3, 'a'), (4, 'b')) (0, 1, 2) View Answer & Explanation Correct Answer: Option B tuple() iterates its argument: a string gives characters, a dict gives its keys. Workspace Report Question page & discussion Report an error in this question Submit Report
Question 13 Hard What is the output of the following Python program? main.py Copy pairs = [("b", 2), ("a", 2), ("c", 1)] print(sorted(pairs)) print(sorted(pairs, key=lambda p: p[1])) A [('a', 2), ('b', 2), ('c', 1)] [('c', 1), ('b', 2), ('a', 2)] B [('a', 2), ('b', 2), ('c', 1)] [('c', 1), ('a', 2), ('b', 2)] C [('c', 1), ('a', 2), ('b', 2)] [('c', 1), ('b', 2), ('a', 2)] D [('b', 2), ('a', 2), ('c', 1)] [('c', 1), ('b', 2), ('a', 2)] View Answer & Explanation Correct Answer: Option A Without a key, tuples compare by the first item. With key=p[1], ("b", 2) and ("a", 2) tie and keep their original order – the sort is stable. Workspace Report Question page & discussion Report an error in this question Submit Report
Question 14 Medium What is the output of the following Python program? main.py Copy t = (1, 2) u = t t += (3,) print(t, u, t is u) A (1, 2, 3) (1, 2) False B (1, 2, 3) (1, 2, 3) True C TypeError D (1, 2, 3) (1, 2) True View Answer & Explanation Correct Answer: Option A Tuples cannot be extended in place, so += builds a new tuple and rebinds t; u still refers to the old one. (For lists, += would change both.) Workspace Report Question page & discussion Report an error in this question Submit Report
Question 15 Easy What is the output of the following Python program? main.py Copy print(list(zip("ab", [1, 2])), dict(zip("ab", [1, 2]))) A [['a', 1], ['b', 2]] {'a': 1, 'b': 2} B [('ab', 1, 2)] {'ab': [1, 2]} C [('a', 'b'), (1, 2)] {'a': 'b', 1: 2} D [('a', 1), ('b', 2)] {'a': 1, 'b': 2} View Answer & Explanation Correct Answer: Option D zip pairs items position by position as tuples, and dict() turns those pairs into key–value entries. Workspace Report Question page & discussion Report an error in this question Submit Report