ProgrammingHardTCS NQT · Infosys HackWithInfy · Product Company SDE
What does the following Python 3 code print? matrix = [[1, 2], [3, 4], [5, 6]] transposed = [[row[i] for row in matrix] for i in range(2)] print(transposed)
- A. [[1, 3], [2, 4], [5, 6]]
- B. [[1, 2], [3, 4], [5, 6]]
- C. [[1, 3, 5], [2, 4, 6]]
- D. [[1, 2, 3], [4, 5, 6]]
Correct answer: C. [[1, 3, 5], [2, 4, 6]]
For each column index i, the outer comprehension gathers that column across all rows, producing the transpose.
More Python Programming questions
- What does the following Python 3 code print? print(list(enumerate(["a", "b", "c"], start=1)))
- What does the following Python 3 code print? print(dict(enumerate(["x", "y", "z"])))
- What does the following Python 3 code print? a = {1, 2, 3} b = {2, 3, 4} print(a & b, a | b, a - b, a ^ b)
- What does the following Python 3 code print? a = frozenset([1, 2, 3]) try: a.add(4) except AttributeError: print("AttributeError")
- What does the following Python 3 code print? s = set([1, 2, 2, 3, 3, 3]) print(sorted(s), len(s))
- What does the following Python 3 code print? class Node: def init(self, v, nxt=None): self.v = v; self.nxt = nxt n3 = Node(3) n2 = Node(2…