🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Context Managers: all 10 questions

Quiz: Context Managers · Question 9 of 10

Why is try/finally alone used less often than with for files?

Why is try/finally alone used less often than with for files?

  1. A.try/finally cannot close files
  2. B.try/finally is slower
  3. C.with does the same job in fewer lines and is harder to get wrong
  4. D.with works only in Python 2
Answer & explanation

Correct answer: with does the same job in fewer lines and is harder to get wrong

Both are correct, but with puts the setup and clean-up in one reusable place.

Can you get the rest right without peeking?

Take the timed Quiz: Context Managers quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →