ProgrammingHardAccenture pattern

What is printed?

What is printed?
def k():
    try:
        return "try"
    except Exception:
        return "except"
    else:
        return "else"
print(k())
  1. A. try
  2. B. else
  3. C. except
  4. D. SyntaxError

Correct answer: A. try

Because the try block returns immediately with no exception raised, control never reaches the else clause (else only runs when the try block finishes WITHOUT hitting a return/exception on its last executed statement being a return exits straight away); the function returns 'try'.

More Python Programming questions

Exams that ask this topic