CBSE · Class 12 · Computer Science
Class 12 Exception Handling in Python Mock Test (CBSE)
5 practice questions from Exception Handling in Python, in the style of your board paper, each with a worked answer and the marks for every step. Then take a full chapter test on paper, snap the pages and get every step marked.
Topics in Exception Handling in Python
- Syntax errors
- Exceptions
- Built-in exceptions
- Raising exceptions: raise and assert
- Handling exceptions with try and except
- The else clause
- The finally clause
Exception Handling in Python practice questions with answers
- Q1Multiple choice1 markWhich of the following Python statements will fail with a SyntaxError when the program is run?
- (a)total = 45 + "38"
- (b)print("Total:", 45 + 38)
- (c)age = int("twelve")
- (d)if marks > 40 print("Pass")
Show answer and step marks
- Correct option: (d) if marks > 40 print("Pass") is missing the colon after the condition, so Python reports a SyntaxError before the program runs.+1
Answer: (d) - Q2Multiple choice1 markA train journey planner program asks the passenger for the distance travelled and prints the fare: try: distance = float(input("Distance covered (km): ")) fare = distance × 2.5 print("Fare:", fare) except ValueError: print("Enter a valid distance") finally: print("Journey record saved") The passenger enters 120 when the input is requested. Which output is printed?
- (a)Fare: 300.0 only
- (b)Enter a valid distance followed by Journey record saved
- (c)Journey record saved only
- (d)Fare: 300.0 followed by Journey record saved
Show answer and step marks
- Correct option: (d) Fare: 300.0 followed by Journey record saved+1
Answer: (d) - Q3Short answer2 marksThe code below is run in IDLE. readings = [12, 15, 9] for i in range(4): print(readings[i] × 2) The program prints 24, 30 and 18, and then stops with an error message. (a) Identify the built-in exception that Python raises when the program stops. [1] (b) State the value of i in the iteration where the program stops. [1]
Show answer and step marks
- Correct: IndexError, because readings[3] asks for a position that does not exist in a list of three elements.+1
- Correct: i = 3, the fourth pass of the loop, which is the first pass with no matching element in readings.+1
Answer: (a) IndexError; (b) i = 3 - Q4Short answer3 marksStudy the code given below. try: num = int(input("Enter a number: ")) print(10 / num) except ZeroDivisionError: print("Cannot divide by zero") The user enters 0 when the input is requested. (a) Name the block of the code in which the statements that may cause an error are written. [1] (b) Name the exception that is handled by the code when the user enters 0. [1] (c) State the message printed by the program when the user enters 0. [1]
Show answer and step marks
- (a) The statements that may cause an error are written in the try block.+1
- (b) The division by zero raises ZeroDivisionError, which is handled by the except block.+1
- (c) The except block prints the message: Cannot divide by zero+1
Answer: (a) try block; (b) ZeroDivisionError; (c) Cannot divide by zero - Q5Short answer3 marksCompare the raise statement and the assert statement in Python. (a) Explain how the raise statement decides whether to raise an exception. [1] (b) Explain how the assert statement decides whether to raise an exception, and state the exception it raises. [1] (c) Write a raise statement that raises a ValueError with the message "Age cannot be negative" when . [1]
Show answer and step marks
- (a) The raise statement raises the named exception unconditionally, as soon as the statement is executed.+1
- (b) The assert statement evaluates the given condition; if the condition is False it raises AssertionError, otherwise the program continues.+1
- (c) if age < 0: raise ValueError("Age cannot be negative")+1
Answer: (a) raise always raises the given exception when reached; (b) assert raises AssertionError only when its condition is False; (c) if age < 0: raise ValueError("Age cannot be negative")
Take a full Exception Handling in Python test
PrepitUp writes a fresh paper on this chapter at the length you pick. Write it on paper with a timer, photograph the pages, and see the marks for every step and what to practise next.
- 15 min10 marks
- 30 min20 marks
- 1.5 hr40 marks
- 3 hr80 marks
Get your Exception Handling in Python answers marked
Sign in, pick this chapter and a length, write the test on paper and get every step marked.
Sign in to take this test