0% found this document useful (0 votes)
18 views33 pages

Exception Handling Reference Material

The document is a comprehensive guide on exception handling in Python for Class 12 students, featuring multiple-choice questions and two-mark questions. It covers various aspects of exception handling, including the use of try, except, else, and finally blocks, as well as specific exceptions like ZeroDivisionError and ValueError. The content is structured for the CBSE Academic Year 2025-26, providing both theoretical explanations and practical examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views33 pages

Exception Handling Reference Material

The document is a comprehensive guide on exception handling in Python for Class 12 students, featuring multiple-choice questions and two-mark questions. It covers various aspects of exception handling, including the use of try, except, else, and finally blocks, as well as specific exceptions like ZeroDivisionError and ValueError. The content is structured for the CBSE Academic Year 2025-26, providing both theoretical explanations and practical examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

**Exception Handling (083 Computer Science) – Class 12**

**CBSE Academic Year 2025–26**

---

## Multiple Choice Questions (MCQs) – 50 Questions (1 mark each)

1. Which of the following is used to handle exceptions in Python?

* a) try-except

* b) if-else

* c) switch-case

* d) for-while

Answer: a) try-except

Reference Year: 2023

2. What does the 'finally' block do in exception handling?

* a) Executes only if an exception occurs

* b) Executes only if no exception occurs

* c) Executes regardless of an exception

* d) Terminates the program

Answer: c) Executes regardless of an exception

Reference Year: 2023


3. Which exception is raised when dividing by zero in Python?

* a) ZeroDivisionError

* b) ValueError

* c) TypeError

* d) IndexError

Answer: a) ZeroDivisionError

Reference Year: 2022

4. What is the purpose of the 'else' block in exception handling?

* a) To handle exceptions

* b) To execute code when no exception occurs

* c) To define a function

* d) To terminate the program

Answer: b) To execute code when no exception occurs

Reference Year: 2022

5. Which of the following is not a built-in exception in Python?

* a) FileNotFoundError

* b) IOError

* c) DivideByZeroError

* d) ValueError

Answer: c) DivideByZeroError
Reference Year: 2021

6. What will be the output of the following code?

```python

try:

x=1/0

except ZeroDivisionError:

print("Division by zero")

finally:

print("Always executed")

```

* a) Division by zero

* b) Always executed

* c) Division by zero\nAlways executed

* d) Error: Division by zero

Answer: c) Division by zero\nAlways executed

Reference Year: 2023

7. Which statement is used to raise an exception manually in Python?

* a) throw

* b) raise

* c) except
* d) catch

Answer: b) raise

Reference Year: 2023

8. What is the output of the following code?

```python

try:

x = int("abc")

except ValueError:

print("ValueError occurred")

except:

print("Some other error occurred")

```

* a) ValueError occurred

* b) Some other error occurred

* c) Error: invalid literal for int()

* d) No output

Answer: a) ValueError occurred

Reference Year: 2022

9. Which exception is raised when trying to access an index that is out of range in a list?

* a) IndexError
* b) KeyError

* c) ValueError

* d) TypeError

Answer: a) IndexError

Reference Year: 2022

10. What does the 'try' block do in exception handling?

* a) Defines a function

* b) Executes code that may raise an exception

* c) Handles exceptions

* d) Terminates the program

Answer: b) Executes code that may raise an exception

Reference Year: 2023

11. Which exception is raised for invalid type operations?

* a) TypeError

* b) IndexError

* c) ValueError

* d) NameError

Answer: a) TypeError

Reference Year: 2023

12. Which of the following will catch any exception?


* a) except Exception:

* b) except:

* c) except ValueError:

* d) except ZeroDivisionError:

Answer: b) except:

Reference Year: 2022

13. What happens if no exception occurs and there is an else block?

* a) Else block is skipped

* b) Else block executes

* c) Program crashes

* d) Finally block is skipped

Answer: b) Else block executes

Reference Year: 2022

14. How can multiple exceptions be caught in one except block?

* a) Using comma-separated exceptions

* b) Using tuple of exceptions

* c) Using multiple excepts only

* d) Using nested try

Answer: b) Using tuple of exceptions

Reference Year: 2023


15. What exception is raised when trying to open a non-existent file?

* a) FileNotFoundError

* b) IOError

* c) ValueError

* d) KeyError

Answer: a) FileNotFoundError

Reference Year: 2023

16. What exception occurs when using an undefined variable?

* a) NameError

* b) ValueError

* c) TypeError

* d) AttributeError

Answer: a) NameError

Reference Year: 2021

17. Which of the following is correct syntax for try-except?

* a) try:

code

except Exception:

handler
* b) try: except

* c) try { } catch { }

* d) try() except()

Answer: a) try:

code

except Exception:

handler

Reference Year: 2023

18. Can finally block be used without except?

* a) Yes

* b) No

* c) Only with else

* d) Only with raise

Answer: a) Yes

Reference Year: 2023

19. Can an exception be raised manually without a message?

* a) Yes

* b) No

* c) Only with ValueError

* d) Only with TypeError

Answer: a) Yes
Reference Year: 2022

20. Which exception occurs when converting an invalid string to integer?

* a) ValueError

* b) TypeError

* c) IndexError

* d) NameError

Answer: a) ValueError

Reference Year: 2022

21. Which of the following is a runtime exception?

* a) ZeroDivisionError

* b) SyntaxError

* c) ImportError

* d) NameError

Answer: a) ZeroDivisionError

Reference Year: 2021

22. Which block is executed always after try?

* a) except

* b) else

* c) finally
* d) raise

Answer: c) finally

Reference Year: 2023

23. Is it mandatory to use except block with try?

* a) Yes

* b) No, can use finally alone

* c) Only with raise

* d) Only in loops

Answer: b) No, can use finally alone

Reference Year: 2023

24. What will happen if multiple except blocks match an exception?

* a) First matching block executes

* b) All matching blocks execute

* c) Program crashes

* d) Finally block skips

Answer: a) First matching block executes

Reference Year: 2023

25. Which of the following is NOT used in Python exception handling?

* a) try
* b) catch

* c) finally

* d) raise

Answer: b) catch

Reference Year: 2022

26. What will be the output of the following code?

```python

try:

a = [1, 2, 3]

print(a[5])

except IndexError:

print("IndexError occurred")

```

* a) 3

* b) IndexError occurred

* c) Error

* d) None

Answer: b) IndexError occurred

Reference Year: 2022

27. Which exception is raised when an operation is performed on incompatible data types?
* a) TypeError

* b) ValueError

* c) ZeroDivisionError

* d) KeyError

Answer: a) TypeError

Reference Year: 2023

28. Can multiple try blocks be nested in Python?

* a) Yes

* b) No

* c) Only two levels

* d) Only with finally

Answer: a) Yes

Reference Year: 2023

29. What happens if an exception occurs in finally block?

* a) It is ignored

* b) It is propagated

* c) Program terminates silently

* d) Except block executes

Answer: b) It is propagated

Reference Year: 2022


30. Which exception occurs when a key is not found in a dictionary?

* a) KeyError

* b) IndexError

* c) NameError

* d) ValueError

Answer: a) KeyError

Reference Year: 2022

31. What is the output of the following?

```python

try:

x = 10 / 2

except ZeroDivisionError:

print("Error")

else:

print("Success")

```

* a) Error

* b) Success

* c) None

* d) ZeroDivisionError

Answer: b) Success
Reference Year: 2023

32. Which exception is raised for invalid function arguments?

* a) TypeError

* b) ValueError

* c) NameError

* d) IndexError

Answer: a) TypeError

Reference Year: 2022

33. What exception is raised when importing a non-existent module?

* a) ImportError

* b) ModuleNotFoundError

* c) ValueError

* d) KeyError

Answer: b) ModuleNotFoundError

Reference Year: 2023

34. Which block is optional in try-except-else-finally structure?

* a) try

* b) except

* c) else
* d) finally

Answer: c) else

Reference Year: 2023

35. Can you use multiple except blocks for a single try?

* a) Yes

* b) No

* c) Only with else

* d) Only with raise

Answer: a) Yes

Reference Year: 2022

36. What will happen if no exception occurs and there is no else block?

* a) Program crashes

* b) Try block executes successfully

* c) Finally executes only

* d) Except executes

Answer: b) Try block executes successfully

Reference Year: 2022

37. Which exception occurs when trying to access an attribute that does not exist?

* a) AttributeError
* b) KeyError

* c) NameError

* d) IndexError

Answer: a) AttributeError

Reference Year: 2021

38. Is it mandatory to use finally block?

* a) Yes

* b) No

* c) Only with raise

* d) Only with else

Answer: b) No

Reference Year: 2023

39. Can you raise a custom exception in Python?

* a) Yes

* b) No

* c) Only built-in exceptions

* d) Only in functions

Answer: a) Yes

Reference Year: 2023

40. Which exception occurs when opening a file in wrong mode?


* a) IOError

* b) FileNotFoundError

* c) ValueError

* d) TypeError

Answer: a) IOError

Reference Year: 2022

41. What is the output of the following code?

```python

try:

print(10/0)

except ZeroDivisionError:

print("Error")

else:

print("No Error")

finally:

print("Done")

```

* a) Error\nDone

* b) No Error\nDone

* c) Done only

* d) Program crashes
Answer: a) Error\nDone

Reference Year: 2023

42. Can finally block override a return statement?

* a) Yes

* b) No

* c) Only in functions

* d) Only in loops

Answer: a) Yes

Reference Year: 2023

43. Which exception occurs when accessing a list element using a string index?

* a) TypeError

* b) ValueError

* c) IndexError

* d) KeyError

Answer: a) TypeError

Reference Year: 2022

44. What is the output of:

```python

try:
a = [1,2]

a[3]

except IndexError:

print("Index error")

```

* a) 1

* b) Index error

* c) Error

* d) None

Answer: b) Index error

Reference Year: 2022

45. Can you have try-finally without except?

* a) Yes

* b) No

* c) Only with raise

* d) Only with else

Answer: a) Yes

Reference Year: 2023

46. Which of the following is a built-in exception?

* a) ZeroDivisionError
* b) CustomError

* c) MyError

* d) None of these

Answer: a) ZeroDivisionError

Reference Year: 2021

47. Which exception occurs for invalid conversion from string to int?

* a) ValueError

* b) TypeError

* c) KeyError

* d) AttributeError

Answer: a) ValueError

Reference Year: 2022

48. What exception occurs for an undefined variable?

* a) NameError

* b) ValueError

* c) IndexError

* d) TypeError

Answer: a) NameError

Reference Year: 2021

49. Which block executes if exception is handled successfully?


* a) else

* b) finally

* c) except

* d) try

Answer: b) finally

Reference Year: 2023

50. Can except block be empty?

* a) No

* b) Yes

* c) Only with pass

* d) Only in functions

Answer: c) Only with pass

Reference Year: 2023

## Two-Mark Questions – 25 Questions (2 marks each)

1. Define exception handling in Python.

Answer: Exception handling in Python is a mechanism to manage runtime errors, allowing the program
to continue execution without crashing. It is implemented using try, except, else, and finally blocks.

Reference Year: 2023

2. What is the difference between except and else blocks in exception handling?

Answer: The except block handles exceptions that occur in the try block, whereas the else block
executes code if no exception occurs.
Reference Year: 2022

3. Explain the purpose of the finally block.

Answer: The finally block ensures that code is executed regardless of whether an exception occurred
or not, useful for cleanup like closing files.

Reference Year: 2023

4. What is the use of the raise statement?

Answer: The raise statement is used to manually trigger an exception in Python.

Reference Year: 2023

5. List two built-in exceptions in Python.

Answer: ValueError, IndexError

Reference Year: 2021

6. Difference between try-except and try-except-finally blocks.

Answer: try-except handles errors, while try-except-finally also ensures the finally block runs.

Reference Year: 2022

7. Explain nested try-except blocks.

Answer: Nested try-except blocks involve placing one try-except block inside another to handle specific
exceptions separately.

Reference Year: 2022

8. Role of else block in exception handling.

Answer: The else block executes code if no exception occurs in the try block.
Reference Year: 2023

9. How can multiple exceptions be handled in a single except block?

Answer: Multiple exceptions can be handled by specifying them as a tuple, e.g., except (TypeError,
ValueError):

Reference Year: 2022

10. What happens if an exception is not caught in a try-except block?

Answer: The exception propagates up the call stack and may terminate the program if not handled.

Reference Year: 2021

11. Explain ZeroDivisionError with an example.

Answer: ZeroDivisionError occurs when dividing a number by zero, e.g., 10/0 raises ZeroDivisionError.

Reference Year: 2023

12. What is IndexError? Give an example.

Answer: IndexError occurs when accessing an index out of the list range, e.g., a=\[1,2]; a\[5] raises
IndexError.

Reference Year: 2022

13. Define ValueError with an example.

Answer: ValueError occurs when a function receives an argument of correct type but invalid value,
e.g., int('abc').

Reference Year: 2022

14. What is TypeError? Provide an example.


Answer: TypeError occurs when an operation is applied to an object of inappropriate type, e.g., '5'+5
raises TypeError.

Reference Year: 2023

15. Explain FileNotFoundError with an example.

Answer: FileNotFoundError occurs when trying to open a non-existent file, e.g., open('nofile.txt')
raises FileNotFoundError.

Reference Year: 2023

16. What is NameError? Give an example.

Answer: NameError occurs when a variable is referenced before assignment, e.g., print(x) without
defining x.

Reference Year: 2021

17. Explain AttributeError with an example.

Answer: AttributeError occurs when an attribute reference fails, e.g., 'abc'.append('d') raises
AttributeError.

Reference Year: 2022

18. Difference between raise and assert statements.

Answer: raise is used to trigger exceptions manually, assert is used to test conditions and raise
AssertionError if False.

Reference Year: 2023

19. Can you handle multiple exceptions separately? Explain.

Answer: Yes, by using multiple except blocks, each handling a specific exception type.

Reference Year: 2022


20. Explain ImportError with an example.

Answer: ImportError occurs when an import statement fails, e.g., import non\_existent\_module
raises ImportError.

Reference Year: 2023

21. Why is exception handling important?

Answer: It prevents program crashes, ensures smooth execution, and provides user-friendly error
messages.

Reference Year: 2023

22. Explain KeyError with an example.

Answer: KeyError occurs when accessing a dictionary key that does not exist, e.g., d = {'a':1}; d\['b']
raises KeyError.

Reference Year: 2022

23. What is the output if finally block contains return statement?

Answer: Finally block executes and can override return from try or except blocks.

Reference Year: 2023

24. How can exception handling improve program reliability?

Answer: By managing errors gracefully, it prevents unexpected termination and ensures consistent
program flow.

Reference Year: 2023

25. Define custom exception and give an example.


Answer: A custom exception is a user-defined exception using class, e.g., class
NegativeError(Exception): pass

Reference Year: 2022

## Program-Based Questions – 10 Questions

1. Write a Python program to handle ZeroDivisionError when dividing two numbers.

Answer:

```python

try:

a = int(input("Enter numerator: "))

b = int(input("Enter denominator: "))

result = a / b

print("Result:", result)

except ZeroDivisionError:

print("Cannot divide by zero")

```

Reference Year: 2023

2. Write a Python program to handle ValueError when converting a string to integer.

Answer:

```python

try:

num = int(input("Enter a number: "))


print("Number is:", num)

except ValueError:

print("Invalid input: Not a number")

```

Reference Year: 2022

3. Write a Python program to handle IndexError when accessing a list element.

Answer:

```python

lst = [1, 2, 3]

try:

index = int(input("Enter index: "))

print(lst[index])

except IndexError:

print("Index out of range")

```

Reference Year: 2022

4. Write a Python program to handle FileNotFoundError when opening a file.

Answer:

```python
try:

f = open("data.txt", "r")

content = f.read()

print(content)

except FileNotFoundError:

print("File not found")

```

Reference Year: 2023

5. Write a Python program to handle TypeError for incompatible data types.

Answer:

```python

try:

result = '5' + 5

except TypeError:

print("Cannot add string and integer")

```

Reference Year: 2022

6. Write a Python program to handle multiple exceptions in a single except block.

Answer:
```python

try:

x = int(input("Enter number: "))

y = 10 / x

except (ValueError, ZeroDivisionError):

print("Invalid input or division by zero")

```

Reference Year: 2023

7. Write a Python program that uses finally block to close a file after reading.

Answer:

```python

try:

f = open("data.txt", "r")

print(f.read())

except FileNotFoundError:

print("File not found")

finally:

f.close()

print("File closed")

```

Reference Year: 2023


8. Write a Python program that raises a custom exception when a negative number is entered.

Answer:

```python

class NegativeError(Exception):

pass

num = int(input("Enter a number: "))

try:

if num < 0:

raise NegativeError("Negative number entered")

print(num)

except NegativeError as e:

print(e)

```

Reference Year: 2022

9. Write a Python program to handle multiple exceptions with different messages.

Answer:

```python

try:

a = int(input("Enter numerator: "))


b = int(input("Enter denominator: "))

result = a / b

lst = [1, 2, 3]

print(lst[result])

except ZeroDivisionError:

print("Cannot divide by zero")

except IndexError:

print("Index out of range")

except ValueError:

print("Invalid input")

```

Reference Year: 2022

10. Write a Python program to demonstrate the use of else block in exception handling.

Answer:

```python

try:

num = int(input("Enter number: "))

except ValueError:

print("Invalid input")

else:

print("You entered:", num)

```
Reference Year: 2023

Five-Mark Questions – 5 Questions

1. Discuss the importance of exception handling in Python with examples.

Answer: Exception handling is important as it prevents program crashes, allows smooth execution, and
provides user-friendly error messages. Example: Handling FileNotFoundError ensures program continues
running even if file does not exist.

Reference Year: 2023

2. Explain the difference between try-except and try-except-finally blocks with examples.

Answer: try-except handles exceptions; try-except-finally also includes a finally block which always
executes. Example: Using finally to close files ensures resources are released.

Reference Year: 2022

3. Analyze a scenario where exception handling improves program reliability.

Answer: Example: A program reading user input and performing division. Without exception handling,
invalid input or division by zero crashes the program. Using try-except ensures program prompts user
again without crashing.

Reference Year: 2023

4. Evaluate the use of custom exceptions in Python with an example.

Answer: Custom exceptions allow defining specific errors relevant to the application. Example: class
NegativeError(Exception): pass allows raising NegativeError for invalid negative inputs.

Reference Year: 2022


5. Discuss best practices for exception handling in Python.

Answer: Best practices include catching specific exceptions, using finally for cleanup, avoiding
exceptions for control flow, logging exceptions, and providing informative messages.

Reference Year: 2023

You might also like