LearnFrenzy
Sign in Take a free test

Python Programming · Topic 16 of 25

Exception Handling Questions and Concepts

  • 4 exercises
  • 50 questions
  • ~60 min
  • Answers explained

Interview Questions

Exception Handling · Study material

Exception Handling – Interview Questions

1. What is an exception?

An object representing an error raised at run time; if not caught, it stops the program with a traceback.

2. Explain try, except, else and finally.

try holds risky code; except handles matching exceptions; else runs if no exception occurred; finally always runs, for clean-up.

3. Does finally run if the try block returns?

Yes – finally runs before the function actually returns.

4. How do you catch several exceptions in one clause?

With a tuple: except (TypeError, ValueError) as e:.

5. Why is a bare except: discouraged?

It catches everything, including KeyboardInterrupt, SystemExit and programming errors such as NameError, hiding bugs.

6. How do you create a custom exception?

Define a class inheriting from Exception (or a subclass): class PaymentError(Exception): pass.

7. What does raise without arguments do?

Re-raises the exception currently being handled.

8. What is exception chaining?

raise NewError(...) from original stores the original exception in __cause__ so both appear in the traceback.

9. What is the base class of all built-in exceptions?

BaseException; ordinary errors derive from Exception.

10. Difference between EAFP and LBYL?

EAFP tries the operation and handles failures; LBYL checks preconditions first. Python favours EAFP.

11. What is the difference between IndexError and KeyError?

IndexError: a sequence index out of range; KeyError: a missing dictionary key. Both are LookupError.

12. When should you use assert?

For internal sanity checks during development – not for input validation, because -O removes asserts.

13. Can you catch a SyntaxError in the same file?

No; the file fails to compile before any of it runs. (A SyntaxError raised by exec or compile at run time can be caught.)

14. What happens to the variable e after except ... as e?

It is deleted at the end of the except block to avoid reference cycles.

15. What does the with statement guarantee?

The context manager's __exit__ runs whether the block finishes normally or with an exception (e.g. files get closed).

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About Python Programming

Why it matters

  • Python is the most popular first language and the default for data science, AI, automation and scripting
  • Campus coding rounds and technical interviews increasingly allow – or prefer – Python
  • "Predict the output" questions on lists, strings, dictionaries and mutable defaults are a favourite in written tests
  • Clean Python with comprehensions, generators and OOP is what product companies expect from freshers

Who it is for

  • School students (Class 11–12 computer science with Python) preparing for board exams and practicals
  • College students (B.Tech, BCA, MCA, B.Sc CS) preparing for semester exams, lab work and viva
  • Freshers preparing for campus placements, coding tests and Python interviews
  • Anyone moving into data science, AI or automation who needs strong Python fundamentals

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • Topic-wise MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs: syntax errors, runtime exceptions and logical bugs
  • Coding problems with solutions and real output, every one run on Python 3 – open any example in the online Python compiler

Asked in

  • Campus placements
  • TCS, Infosys, Wipro, Accenture tests
  • Python interviews for freshers
  • Data science interviews
  • CBSE / university practicals
  • GATE CS

How to use this section

  1. Read the study material of a topic and run the examples in the browser.
  2. Practise the four exercises – MCQs, output, errors, coding problems – each marked Easy, Medium or Hard, and read every explanation.
  3. Revise with the interview questions and cheat sheet, then move to the next topic.

Search LearnFrenzy

Esc

Quick links