ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
def f():
    try:
        return 'try'
    finally:
        print('finally', end=' ')
print(f())
  1. A. try finally
  2. B. try
  3. C. finally
  4. D. finally try

Correct answer: D. finally try

finally runs before the return value is handed back to the caller, so its print appears first. The function still returns 'try' and print shows that value after the finally output.

More Python Programming questions

Exams that ask this topic