ProgrammingHardTCS CodeVita · GATE CS · Advanced
What does the following Python 3 code print? while False: print("never") else: print("while-else")
- A. never while-else
- B. (nothing printed)
- C. while-else
- D. never
Correct answer: C. while-else
A while loop's else runs whenever the loop finishes without break, including when the body never executes at all.
More Python Programming questions
- What does the following Python 3 code print? x = 1 while x < 5: x += 1 if x == 3: break else: print("no-break") print(x)
- What does the following Python 3 code print? print([i for i in range(5) if i != 2][::-1])
- 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)]…
- 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)