Skip to content
CodeKruPro

What does a with statement guarantee?

QuizMediumPython onlyAsked in: Infosys, TCS

A with statement uses a context manager: an object with __enter__ (runs at the start) and __exit__ (runs at the end). Which statements about with are true? Pick all that apply.

  • __exit__ runs even if the block raises an exception
  • If __exit__ returns a false value, the exception continues after clean-up
  • with open(path) as f: closes the file when the block ends
  • The value after as is always the object written after with

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in