Completed
try/except/else/finally - combined exception scenarios
Questions From This Challenge
Q1mcq1 mark
What is the purpose of the else clause in a try-except-else block?
It runs only if an exception occurs in the try block
It runs only if no exception occurs in the try block ✓
It always runs regardless of whether an exception occurred
It replaces the except block entirely
Q2mcq1 mark
What is the purpose of the finally clause in a try-except-finally block?
It runs only when an exception occurs
It runs only when no exception occurs
It always runs, whether or not an exception occurred ✓
It runs only if the else block also runs
Q3mcq1 mark
In a try-except-else-finally block, if an exception occurs in the try block and is caught by except, does the else block execute?
Yes, it always executes
No, else is skipped whenever an exception was raised and caught ✓
Only if finally also runs
Only if the exception is printed
Q4mcq1 mark
Given a try block that raises no exception, in what order do the else and finally blocks execute?
finally runs, then else
else runs, then finally ✓
Only finally runs; else is skipped
Only else runs; finally is skipped
Q5mcq1 mark
If an exception occurs in the try block that is NOT caught by any except clause, does the finally block still execute before the error propagates further?
Yes, finally always executes before the exception propagates ✓
No, finally is skipped since the exception was never handled
Only if an else clause is also present
finally never executes when an exception occurs
Q6mcq1 mark
What is the correct order of clauses when using try, except, else, and finally together in one statement?
try, else, except, finally
try, except, else, finally ✓
try, finally, except, else
try, except, finally, else
Q7output1 mark
What is the output?
try:
x = 20 / 4
except ZeroDivisionError:
print("Error")
else:
print("Success:", x)
finally:
print("Execution complete")
Correct output:
Success: 5.0
Execution completeQ8output1 mark
What is the output?
try:
x = 20 / 0
except ZeroDivisionError:
print("Error occurred")
else:
print("No error")
finally:
print("Execution complete")
Correct output:
Error occurred
Execution completeQ9output1 mark
What is the output?
data = {"a": 1, "b": 2}
try:
val = data["c"]
except KeyError:
print("Missing key")
else:
print("Found:", val)
finally:
print("Lookup done")
Correct output:
Missing key
Lookup doneQ10output1 mark
What is the output?
data = {"a": 1, "b": 2}
try:
val = data["a"]
except KeyError:
print("Missing key")
else:
print("Found:", val)
finally:
print("Lookup done")
Correct output:
Found: 1
Lookup doneQ11output1 mark
What is the output?
nums = [10, 20, 30]
try:
print(nums[5])
except IndexError:
print("Index error")
except ZeroDivisionError:
print("Zero div error")
else:
print("No problem")
finally:
print("Cleanup")
Correct output:
Index error
CleanupQ12output1 mark
What is the output?
try:
total = "Marks: " + 95
except TypeError:
print("Type error caught")
else:
print("No error occurred")
finally:
print("Process ended")
Correct output:
Type error caught
Process endedQ13output1 mark
What is the output?
try:
age = int("17")
except ValueError:
print("Invalid input")
else:
print("Age accepted:", age)
finally:
print("Input check done")
Correct output:
Age accepted: 17
Input check doneQ14output1 mark
What is the output?
try:
print("Start")
a = 8
b = 2
c = a / b
except ZeroDivisionError:
print("Cannot divide")
else:
print("Division result:", c)
finally:
print("Block finished")
print("Outside try")
Correct output:
Start
Division result: 4.0
Block finished
Outside tryQ15output1 mark
What is the output?
try:
scores = [88, 92]
print("First score:", scores[0])
print("Third score:", scores[2])
print("This will not print")
except IndexError:
print("Index out of range")
else:
print("All scores printed")
finally:
print("Score check complete")
Correct output:
First score: 88
Index out of range
Score check complete