ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
a = [1, 2, 3] b = a b += [4] c = a + [5] a = a * 2 print(b, c, a)
- A. [1, 2, 3] [1, 2, 3, 5] [1, 2, 3, 1, 2, 3]
- B. [1, 2, 3, 4] [1, 2, 3, 4, 5] [1, 2, 3, 4, 1, 2, 3, 4]
- C. [1, 2, 3, 4] [1, 2, 3, 5] [1, 2, 3, 4, 1, 2, 3, 4]
- D. [1, 2, 3, 4] [1, 2, 3, 4, 5] [1, 2, 3, 1, 2, 3]
Correct answer: B. [1, 2, 3, 4] [1, 2, 3, 4, 5] [1, 2, 3, 4, 1, 2, 3, 4]
b += [4] changes the list that a and b both name. c is built from that list before a is rebound, and a * 2 creates a new list for a.
More Python Programming questions
- What is printed? python def stepstocycle(f, s): seen = {} i = 0 while s not in seen: seen[s] = i s = f[s] i += 1 return i, i - seen[s]…
- What is printed? python from collections import Counter def divpairs(a, k): c = Counter(x % k for x in a) total = 0 for r in c: s = (k…
- What is printed? python def isrot(a, b): return len(a) == len(b) and b in a + a print(isrot('abcde', 'cdeab'), isrot('abc', 'acb')…
- What is printed? python def egcd(a, b): if b == 0: return a, 1, 0 g, x, y = egcd(b, a % b) return g, y, x - (a // b) y g, x, y = egcd(240…
- What is printed? python def canjump(a): far = 0 for i, x in enumerate(a): if i > far: return False far = max(far, i + x) return True…
- What is printed? python import copy a = [[1], [2]] b = a[:] c = copy.deepcopy(a) a[0].append(9) print(b, c)