1. Home
  2. Data Science
  3. Python
  4. What is the output of the following Python code snippet?

Added 25 Nov 2026

What is the output of the following Python code snippet?

class CM:
def __enter__(self):
print('enter', end=' ')
def __exit__(self, *a):
print('exit')
with CM():
pass
  1. enter exit Correct answer
  2. exit enter
  3. enter
  4. Error

Correct answer

A. enter exit

7 views

MCQSEXAM

Explanation

__enter__ runs on entering with and __exit__ on leaving.

Reference

Asked in: Python Interview OOP

https://docs.python.org/3/tutorial/classes.html

Preparing for an exam?

Doubts & discussion

Not sure why the answer is A? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More Python MCQs

Practise all Python MCQs

Similar MCQs