ProgrammingModerateGATE CS · Product-company placement
What is printed?
What is printed?
x = [0, 1, 2, 3, 4, 5] print(x[1::2], x[-1::-2], x[::-1][::2] == x[::-2])
- A. [1, 3, 5] [5, 3, 1] False
- B. [0, 2, 4] [5, 3, 1] True
- C. [1, 3, 5] [5, 3, 1] True
- D. [1, 3, 5] [4, 2, 0] True
Correct answer: C. [1, 3, 5] [5, 3, 1] True
x[1::2] takes the odd positions. Stepping -2 from the last element gives 5, 3, 1. Reversing and then taking every second element is the same as stepping by -2 from the end.
More Python Programming questions
- What is printed? python m = [[1, 2, 3], [4, 5, 6]] t = list(zip(m)) print(t, [list(r) for r in zip(t)] == m, [x for row in m for x in…
- What is printed? python 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]…
- 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]…