exec() — Python Technical Interview Questions & Answers
exec()
exec()
- exec() is built into Python. You do not import it. Freshers only need one honest call and the printed result — not a long definition.
- exec() — output: 3. exec runs statements. Still unsafe on user text.
- Wrong type often raises TypeError. Empty values matter for bool/all/any. Don’t mix exec up with a similar name (sorted vs sort, int vs float).
Technical Interview Tip: Call exec() once and say what prints.
Code Examples for exec()
Example
# exec()
exec("x = 2\nprint(x + 1)")
exec() — output: 3. exec runs statements. Still unsafe on user text.
How to Explain exec() to an Interviewer
Explain exec() as if you are teaching a junior — definition, then one tiny script.
exec() is built-in — no import. One call + exact output.
What does the example print, and why?
exec() — output: 3
What mistake do freshers make with exec()?
Wrong type → TypeError.