ProgrammingModerateTCS NQT · Accenture

What is printed?

What is printed?
def f(x):
    out = []
    try:
        out.append('t')
        if x:
            raise ValueError
    except ValueError:
        out.append('e')
    else:
        out.append('l')
    finally:
        out.append('f')
    return ''.join(out)
print(f(0), f(1))
  1. A. tlf tlf
  2. B. tlf tef
  3. C. tle tef
  4. D. tef tlf

Correct answer: B. tlf tef

The else clause runs only when no exception occurred, and finally always runs last. f(0) gives t, l, f and f(1) gives t, e, f.

More Python Programming questions

Exams that ask this topic