Exception Handling in Python · Class 12 · CBSE · Science Computer Science

What is printed by the following Python code? try: number = int("abc") except ValueError: print("A") except TypeError: print("B")

Practice question from Exception Handling in Python (Class 12 · CBSE · Science Computer Science), with the correct answer and a step-by-step solution.

easy · MCQ · Q-E4AC3C

What is printed by the following Python code? try: number = int("abc") except ValueError: print("A") except TypeError: print("B")

  1. A.ACorrect answer
  2. B.B
  3. C.abc
  4. D.The program terminates without output

Solution

int("abc") raises a ValueError because the string cannot be converted to an integer. The matching except block prints A.

More from Exception Handling in Python

All of Exception Handling in Python

Practise more Computer Science

Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.

Start practising free

Free to start · No credit card needed