ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
grid = [[0] * 3 for _ in range(3)]
for i in range(3):
grid[i][i] = grid[i - 1][i - 1] + 1
print(grid[0][0], grid[2][2], sum(map(sum, grid)))- A. 1 3 5
- B. 2 4 9
- C. 1 2 4
- D. 1 3 6
Correct answer: D. 1 3 6
On i = 0, grid[-1][-1] (the bottom-right cell, still 0) is read, so grid[0][0] = 1; then grid[1][1] = 2 and grid[2][2] = 3, overwriting the corner after it was used. Total = 1 + 2 + 3 = 6.
More Python Programming questions
- What is printed? python a = [1, 2, 3] b = a a += a c = [1, 2] c.append(c) print(b, len(c), c[2][2][0], c)
- What is printed? python s = "The quick brown fox" w = s.split() print(" ".join(w[::-1]), " ".join(x[::-1] for x in w)[:9], max(w, key=len))
- What is printed? python d = {} for w in "to be or not to be".split(): d[w] = d.get(w, 0) + 1 best = max(d, key=d.get) print(best…
- What is printed? python class Stack(list): def push(self, v): self.append(v) return self s = Stack() s.push(1).push(2) t = s + [3]…
- What is printed? (Output lines are shown separated by " / ".) python def f(, a, b=2): return a - b def g(a, b=2, /): return a - b…
- What is printed? python def outer(fn): def w(a, k): return fn(a, k) 2 return w class C: @outer def val(self, x): return x + 1 @property…