ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
out = []
for i in range(3):
    try:
        if i == 1:
            continue
        out.append(i)
    finally:
        out.append('f')
print(out)
  1. A. [0, 'f', 2, 'f']
  2. B. [0, 'f', 'f', 'f', 2]
  3. C. [0, 'f', 'f', 2, 'f']
  4. D. [0, 2, 'f', 'f']

Correct answer: C. [0, 'f', 'f', 2, 'f']

A finally block runs even when the try body executes continue, so the marker is added on every pass. The continue only skips the append of 1.

More Python Programming questions

Exams that ask this topic