Completed
Exception Basics - try/except introduction
Questions From This Challenge
Q1mcq1 mark
What is the primary purpose of a try-except block in Python?
To make the program run faster
To handle runtime errors gracefully without crashing the program ✓
To declare variables before they are used
To define a new function
Q2mcq1 mark
Which exception is raised when a program attempts to divide a number by zero, e.g. `10 / 0`?
ValueError
ZeroDivisionError ✓
TypeError
IndexError
Q3mcq1 mark
Which exception is raised by the following code?
num = int("seven")
TypeError
KeyError
ValueError ✓
IndexError
Q4mcq1 mark
Which exception is raised by the following code?
marks = [70, 80, 90]
print(marks[10])
IndexError ✓
KeyError
ValueError
TypeError
Q5mcq1 mark
Which exception is raised by the following code?
student = {"name": "Riya"}
print(student["grade"])
IndexError
ValueError
KeyError ✓
TypeError
Q6mcq1 mark
Which exception is raised by the following code?
result = "Total: " + 100
ValueError
IndexError
KeyError
TypeError ✓
Q7mcq1 mark
In a try-except block, if no exception occurs while executing the try block, what happens to the except block?
It executes anyway after the try block
It is skipped entirely ✓
It raises a SyntaxError
The program terminates immediately
Q8mcq1 mark
Consider:
try:
print("A")
print(10 / 0)
print("B")
except ZeroDivisionError:
print("C")
Which lines are actually printed?
A, B, C
A, C ✓
A, B
C only
Q9output1 mark
What is the output?
try:
x = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
Correct output:
Cannot divide by zeroQ10output1 mark
What is the output?
try:
a = [1, 2, 3]
print(a[5])
except IndexError:
print("Index out of range")
print("Program continues")
Correct output:
Index out of range
Program continuesQ11output1 mark
What is the output?
d = {"name": "Amit", "age": 17}
try:
print(d["marks"])
except KeyError:
print("Key not found")
Correct output:
Key not foundQ12output1 mark
What is the output?
try:
num = int("25a")
print(num)
except ValueError:
print("Invalid number")
print("Done")
Correct output:
Invalid number
DoneQ13output1 mark
What is the output?
try:
result = "5" + 5
print(result)
except TypeError:
print("Type mismatch")
Correct output:
Type mismatchQ14output1 mark
What is the output?
try:
marks = [90, 85, 78]
print(marks[1])
print(10 / 0)
except IndexError:
print("Index problem")
except ZeroDivisionError:
print("Division problem")
Correct output:
85
Division problemQ15output1 mark
What is the output?
try:
x = 15
y = 3
print(x // y)
except ZeroDivisionError:
print("Cannot divide")
print("End")
Correct output:
5
End