CBSE Class 12
Computer Science
01 Exception Handling in Python
30
Total
0
Attempted
0
Correct
0
Wrong
00:00:00
Q-1
What is the purpose of exception handling in Python?
Easy
Introduction
A
To handle runtime errors gracefully
B
To stop the program immediately
C
To improve compilation speed
D
To avoid writing functions
Answer: To handle runtime errors gracefully
Explanation: Exception handling allows a program to deal with runtime errors without crashing.
Q-2
Which keyword is used to handle exceptions in Python?
Easy
Handling Exceptions
A
try
B
except
C
handle
D
catch
Answer: except
Explanation: The except block is used to catch and handle exceptions.
Q-3
Which of the following errors occurs during program execution?
Easy
Exceptions
A
Syntax Error
B
Indentation Error
C
Exception
D
Compilation Error
Answer: Exception
Explanation: Exceptions occur during runtime when the program encounters an unexpected condition.
Q-4
Which of the following is a syntax error?
Easy
Syntax Errors
Reference Code
```python
print("Hello"
A
Division by zero
B
Accessing missing file
C
Invalid indentation
D
Missing closing parenthesis
Answer: Missing closing parenthesis
Explanation: A missing closing parenthesis causes a syntax error before execution.
Q-5
What will happen if an exception occurs and it is not handled?
Easy
Exceptions
A
Program terminates with error
B
Program continues normally
C
Python ignores the error
D
Code recompiles automatically
Answer: Program terminates with error
Explanation: If an exception is not handled, Python stops the program and displays an error message.
Q-6
Which block is used to test code that may raise an exception?
Easy
Handling Exceptions
A
except
B
try
C
error
D
finally
Answer: try
Explanation: The try block contains code that may cause an exception.
Q-7
Which built-in exception occurs when dividing by zero?
Easy
Built-in Exceptions
A
ValueError
B
TypeError
C
ZeroDivisionError
D
IndexError
Answer: ZeroDivisionError
Explanation: ZeroDivisionError occurs when division or modulo by zero happens.
Q-8
What will be the output of the following code?
Easy
Handling Exceptions
Reference Code
```python
try:
print(10/0)
except ZeroDivisionError:
print("Error handled")
A
10
B
0
C
No output
D
Error handled
Answer: Error handled
Explanation: Division by zero raises ZeroDivisionError which is handled by the except block.
Q-9
Which exception occurs when a variable is not defined?
Medium
Built-in Exceptions
A
NameError
B
TypeError
C
IndexError
D
ValueError
Answer: NameError
Explanation: NameError occurs when a variable name is not defined.
Q-10
Which keyword is used to raise an exception manually?
Medium
Raising Exceptions
A
raise
B
throw
C
except
D
error
Answer: throw
Explanation: The raise keyword is used to trigger an exception intentionally.
Q-11
What exception occurs when accessing a list element with an invalid index?
Easy
Built-in Exceptions
A
KeyError
B
ValueError
C
IndexError
D
TypeError
Answer: IndexError
Explanation: IndexError occurs when the list index is out of range.
Q-12
Which block always executes whether an exception occurs or not?
Easy
Finally Clause
A
try
B
except
C
raise
D
finally
Answer: finally
Explanation: The finally block always executes after try and except.
Q-13
What will be printed?
Medium
Handling Exceptions
Reference Code
```python
try:
x = int("abc")
except ValueError:
print("Invalid number")
A
Invalid number
B
abc
C
ValueError
D
Nothing
Answer: Invalid number
Explanation: Converting 'abc' to int raises ValueError which is handled.
Q-14
Which exception occurs when a wrong data type is used in an operation?
Easy
Built-in Exceptions
A
ValueError
B
TypeError
C
IndexError
D
NameError
Answer: TypeError
Explanation: TypeError occurs when an operation is applied to an incompatible type.
Q-15
Which statement is used to define custom error conditions?
Medium
Raising Exceptions
A
try
B
except
C
raise
D
pass
Answer: raise
Explanation: raise allows programmers to trigger custom exceptions.
Q-16
What will be the output?
Medium
Finally Clause
Reference Code
```python
try:
print("Hello")
finally:
print("Done")
A
Done
B
Error
C
Hello
D
Hello
Done
Answer: Hello
Done
Explanation: Both statements execute because finally runs after try.
Q-17
Which block executes when no exception occurs?
Medium
Handling Exceptions
A
else
B
finally
C
except
D
raise
Answer: else
Explanation: The else block runs if no exception occurs in the try block.
Q-18
Which error occurs when a dictionary key is not found?
Medium
Built-in Exceptions
A
ValueError
B
KeyError
C
TypeError
D
IndexError
Answer: KeyError
Explanation: KeyError occurs when accessing a dictionary key that does not exist.
Q-19
Which statement is correct about exceptions?
Easy
Exceptions
A
They occur only during compilation
B
They occur only in loops
C
They occur during runtime
D
They never stop a program
Answer: They occur during runtime
Explanation: Exceptions occur during program execution.
Q-20
Which keyword is used with try to catch errors?
Easy
Handling Exceptions
A
try
B
error
C
catch
D
except
Answer: except
Explanation: except is used with try to catch exceptions.
Q-21
Which error occurs when converting an invalid string to integer?
Medium
Built-in Exceptions
A
ValueError
B
TypeError
C
NameError
D
IndexError
Answer: ValueError
Explanation: ValueError occurs when a function receives a valid type but inappropriate value.
Q-22
Which block can follow multiple except blocks?
Medium
Finally Clause
A
try
B
finally
C
raise
D
error
Answer: finally
Explanation: finally executes after try and except blocks.
Q-23
What will be the output?
Medium
Handling Exceptions
Reference Code
```python
try:
print(5 + '5')
except TypeError:
print("Type Error")
A
10
B
55
C
Type Error
D
5
Answer: Type Error
Explanation: Adding integer and string raises TypeError.
Q-24
Which statement stops exception propagation by handling it?
Medium
Handling Exceptions
A
try
B
raise
C
pass
D
except
Answer: except
Explanation: The except block catches and handles exceptions.
Q-25
Which block contains code that may cause an error?
Easy
Handling Exceptions
A
try
B
except
C
else
D
finally
Answer: try
Explanation: The try block contains risky code.
Q-26
Which keyword allows you to trigger exceptions deliberately?
Easy
Raising Exceptions
A
except
B
raise
C
try
D
finally
Answer: raise
Explanation: raise allows programmers to generate exceptions.
Q-27
Which exception occurs when accessing a file that does not exist?
Medium
Built-in Exceptions
A
ValueError
B
IndexError
C
FileNotFoundError
D
KeyError
Answer: FileNotFoundError
Explanation: FileNotFoundError occurs when a file cannot be located.
Q-28
What will be printed?
Hard
Finally Clause
Reference Code
```python
try:
print("Start")
x = 10/0
except:
print("Error")
finally:
print("End")
A
Start
B
Error
C
End
D
Start
Error
End
Answer: Start
Error
End
Explanation: All three lines execute: Start, Error, and End.
Q-29
Which clause executes when no exception occurs in try block?
Medium
Handling Exceptions
A
else
B
finally
C
except
D
raise
Answer: else
Explanation: else executes only when no exception occurs.
Q-30
Which of the following is a built-in Python exception?
Easy
Built-in Exceptions
A
ErrorType
B
IndexError
C
ExceptionType
D
PythonError
Answer: IndexError
Explanation: IndexError is a standard built-in Python exception.
◀
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
▶