Completed
Mixed: Functions + Exceptions - combined application
Questions From This Challenge
Q1mcq1 mark
A function performs a division without any internal error handling. If the caller wraps the function call in a try-except block, and the function raises a ZeroDivisionError, where is the exception actually caught?
Inside the function itself, automatically
In the caller's except block, since the function does not handle it ✓
The program crashes regardless of the caller's try-except
It is silently ignored and no code runs
Q2mcq1 mark
A function defines a local variable inside its try block. After the function returns, can the caller access that variable directly by name?
Yes, it becomes global automatically
No, variables defined inside a function have local scope and are not accessible outside it ✓
Only if it was defined inside the except block
Only if the function uses a finally clause
Q3mcq1 mark
Given `def compute(a, b=0):`, which of these calls executes without raising a TypeError for missing or extra arguments?
compute(5) ✓
compute()
compute(5, 6, 7)
compute(5, 6, 7, 8)
Q4mcq1 mark
A function returns a value inside its try block, and returns a different value inside its except block. If the try block succeeds without error, which return statement executes?
Both return statements execute, one after the other
Only the try block's return statement executes ✓
Only the except block's return statement executes
Neither executes; the function returns None
Q5mcq1 mark
A function contains its own try-except that catches every exception it might raise internally. If no exception ever escapes the function, what happens to a caller's try-except block wrapped around the call?
The caller's except block still executes anyway
The caller's except block is skipped, since no exception ever propagated out of the function ✓
A SyntaxError occurs because of the double handling
The program stops immediately after calling the function
Q6mcq1 mark
Given `def show_result(name, marks):`, which call correctly supplies both arguments using only positional arguments, matching name first and marks second?
show_result("Aditi", 88) ✓
show_result(88, "Aditi")
show_result("Aditi")
show_result("Aditi", 88, 90)
Q7output1 mark
What is the output?
def divide(a, b):
try:
result = a / b
except ZeroDivisionError:
result = "undefined"
return result
print(divide(20, 4))
print(divide(20, 0))
Correct output:
5.0
undefinedQ8output1 mark
What is the output?
def get_item(lst, index):
return lst[index]
items = [10, 20, 30]
try:
print(get_item(items, 1))
print(get_item(items, 5))
except IndexError:
print("Invalid index")
Correct output:
20
Invalid indexQ9output1 mark
What is the output?
def calculate_average(total, count=0):
try:
return total / count
except ZeroDivisionError:
return 0
print(calculate_average(100, 5))
print(calculate_average(100))
Correct output:
20.0
0Q10output1 mark
What is the output?
value = 100
def modify(value):
try:
value = value / 0
except ZeroDivisionError:
value = -1
return value
print(modify(50))
print(value)
Correct output:
-1
100Q11output1 mark
What is the output?
def check_key(d, key):
try:
result = d[key]
except KeyError:
return "not found"
else:
return result
student = {"name": "Neha", "grade": "A"}
print(check_key(student, "grade"))
print(check_key(student, "age"))
Correct output:
A
not foundQ12output1 mark
What is the output?
def add_values(a, b):
return a + b
try:
print(add_values(5, 10))
print(add_values("5", 10))
except TypeError:
print("Cannot add these types")
Correct output:
15
Cannot add these typesQ13output1 mark
What is the output?
def process(n):
try:
result = 100 / n
except ZeroDivisionError:
result = None
finally:
print("Processed:", n)
return result
print(process(10))
print(process(0))
Correct output:
Processed: 10
10.0
Processed: 0
NoneQ14output1 mark
What is the output?
def convert_to_int(text, default=0):
try:
return int(text)
except ValueError:
return default
print(convert_to_int("42"))
print(convert_to_int("abc", 99))
Correct output:
42
99Q15output1 mark
What is the output?
def safe_lookup(prices, item):
try:
cost = prices[item]
except KeyError:
cost = -1
else:
cost = cost + 10
return cost
prices = {"pen": 5, "book": 50}
try:
print(safe_lookup(prices, "book"))
print(safe_lookup(prices, "eraser"))
except KeyError:
print("This will never print")
Correct output:
60
-1