ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
x = [1, 2, 3] y = x x[:] = [7] x = x + [8] print(y, x)
- A. [1, 2, 3] [7, 8]
- B. [7] [7, 8]
- C. [7, 8] [7, 8]
- D. [7] [7]
Correct answer: B. [7] [7, 8]
Slice assignment x[:] = [7] replaces the contents of the shared object (so y sees [7]); x = x + [8] then creates a new list bound only to x.
More Python Programming questions
- What is printed? python m = [[0] 2 for in range(2)] n = m.copy() n[0][0] = 5 n[1] = [6, 6] print(m)
- What is printed? python d = {"k": []} e = dict(d) f = {k: v[:] for k, v in d.items()} d["k"].append(1) e["k"] += [2] f["k"] += [3]…
- What is printed? python s = "abc" t = s s += "d" L = [1] M = L L += [2] print(t, M)
- What is printed? python row = [0] 3 grid = [row, row[:], row] grid[0][1] = 4 grid[1][2] = 7 print(grid[2], grid[1])
- What is printed? python def f(x, acc={}): acc[x] = len(acc) return acc a = f("p") b = f("q", {}) c = f("r") print(a is c, a, b)
- What is printed? python def f(n, seen=[]): if n in seen: return seen seen = seen + [n] return seen print(f(1), f(2), f(1))