ProgrammingHardIBM · Deloitte · Product Company SDE

What does the following Python 3 code print? def f(): for i in range(5): if i == 3: break else: print("else-no-break") print(i) f()

  1. A. else-no-break 3
  2. B. 3
  3. C. 4
  4. D. 2

Correct answer: B. 3

The loop's else clause only runs if the loop completes without break; since break fires at i==3, it is skipped, and i keeps its value 3.

More Python Programming questions

Exams that ask this topic