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")
- A.ACorrect answer
- B.B
- C.abc
- 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
- What is the main purpose of the finally block in Python exception handling?
- What will be the output of the following Python code? try: value = int("10") except ValueError: print("invalid") else: print(value / 4) fin…
- Explain why specific exception classes should generally be handled before a general exception class such as Exception. Include a short Pyth…
- What is printed by the following Python code? try: try: int("x") except ValueError: raise RuntimeError("bad input") except RuntimeError as…
Practise more Computer Science
Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.
Start practising freeFree to start · No credit card needed