ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
a = [5, 1, 4] b = a a = sorted(a) c = a.copy() c.reverse() print(a, b, c)
- A. [5, 1, 4] [5, 1, 4] [1, 4, 5]
- B. [1, 4, 5] [5, 1, 4] [1, 4, 5]
- C. [1, 4, 5] [5, 1, 4] [5, 4, 1]
- D. [1, 4, 5] [1, 4, 5] [5, 4, 1]
Correct answer: C. [1, 4, 5] [5, 1, 4] [5, 4, 1]
sorted returns a new list, so a is rebound to a new object and b still refers to the original [5, 1, 4]. copy() makes a fresh list, and reverse() flips only that copy.
More Python Programming questions
- What is printed? python def f(a, b=[]): b.append(a) return len(b) print(f(1), f(2, []), f(3))
- What is printed? python values = [4, 8, 15, 16, 23, 42] print(bisect.bisect(values, 15) if False else 0, [v for v in values if v % 2 and v…
- What is printed? python import heapq h = [5, 1, 4] print(heapq.heappushpop(h, 0), heapq.heappushpop([], 7), sorted(h))
- What is printed? python def walk(n): if n == 0: return [] return [n] + walk(n - 1) print(walk(3), len(walk(500)))
- What is printed? python import itertools rows = [[1, 2], [3], [4, 5, 6]] print(sum(len(r) for r in rows), list(itertools.chain(rows))[-1]…
- What is printed? python x = 10 def g(): print(x) x = 20 try: g() except UnboundLocalError as e: print(type(e).name)