LearnFrenzy
Sign in Take a free test

Exception Handling · Exercise 3 of 4

Exception Handling Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Each Python program handles exceptions incorrectly: a syntax error in the try statement, a wrong exception type, an over-broad handler that hides a bug, or code that relies on a failed try. Choose the option that names the real problem.

Question 1 Easy

The program below stops with an error. Which statement correctly describes the problem?

main.py
try:
    age = int("twenty")
except TypeError:
    age = 0
print(age)
A It prints 0
B It prints 20
C ValueError is not caught: int("twenty") raises ValueError, but the handler only catches TypeError
D SyntaxError
Question page & discussion
Question 2 Easy

Python refuses to run the program below. Which statement correctly describes the problem?

main.py
try:
    result = 10 / 0
print("finished")
A Division by zero is a syntax error
B print must be indented
C result must be declared
D A try block must be followed by at least one except or a finally clause
Question page & discussion
Question 3 Medium

Python refuses to run the program below. Which statement correctly describes the problem?

main.py
try:
    n = int("12")
else:
    print("ok", n)
except ValueError:
    print("bad")
A else is not allowed with try
B n is not defined
C int("12") is invalid
D The order must be try, except, else, finally; else cannot come before except
Question page & discussion
Question 4 Medium

The text "42" is a valid number, yet the program prints "Invalid number". Which statement correctly identifies the bug?

main.py
value = "42"
try:
    number = int(valu)
except:
    print("Invalid number")
else:
    print("Number:", number)
A The typo valu raises NameError, and the bare except catches it as if the input were bad; catch only ValueError so real bugs surface
B int() cannot convert "42"
C else runs before except
D number must be global
Question page & discussion
Question 5 Medium

A non-numeric quantity should print "Quantity must be a number". Which statement correctly identifies the bug?

main.py
qty_text = "ten"
try:
    qty = int(qty_text)
except Exception:
    print("Something went wrong")
except ValueError:
    print("Quantity must be a number")
A int("ten") raises TypeError
B Only one except clause is allowed
C except Exception comes first and also matches ValueError, so the specific handler is unreachable; put except ValueError first
D The handlers run in reverse order
Question page & discussion
Question 6 Easy

The program below stops with an error. Which statement correctly describes the problem?

main.py
def set_age(age):
    if age < 0:
        raise "Age cannot be negative"
    return age

set_age(-3)
A It prints Age cannot be negative
B ValueError
C TypeError: only exception objects can be raised; write raise ValueError("Age cannot be negative")
D SyntaxError
Question page & discussion
Question 7 Hard

The program below stops with an error. Which statement correctly describes the problem?

main.py
try:
    port = int("80a")
except ValueErorr:
    port = 8080
print(port)
A It prints 8080
B NameError: when the ValueError occurs, Python evaluates the except expression and ValueErorr does not exist; spell it ValueError
C SyntaxError at compile time
D It prints 80
Question page & discussion
Question 8 Hard

A transfer to an unknown account should leave both balances unchanged. Which statement correctly identifies the bug?

main.py
accounts = {"asha": 1000, "ravi": 500}

def transfer(src, dst, amount):
    try:
        accounts[src] -= amount
        accounts[dst] += amount
    except KeyError:
        print("Unknown account:", dst)

transfer("asha", "meera", 300)
print(accounts)
A The debit happens before the failing credit, and the handler does not undo it; validate both accounts (or roll back) before changing any balance
B KeyError is the wrong exception
C dict values cannot be changed in a function
D print shows an old copy of accounts
Question page & discussion
Question 9 Medium

The program prints "bad input" and then stops with an error. Which statement correctly describes the problem?

main.py
try:
    value = int("abc")
except ValueError:
    print("bad input")
print(value * 2)
A It prints bad input and 0
B TypeError
C ValueError is raised again
D NameError: int() failed before value was assigned, and the handler did not give it a default
Question page & discussion
Question 10 Hard

The program below stops with an error. Which statement correctly describes the problem?

main.py
def check(stock):
    if stock < 0:
        raise
    return stock

check(-1)
A It returns -1
B ValueError
C SyntaxError
D RuntimeError: a bare raise re-raises the active exception, and there is none; raise a specific exception such as ValueError("negative stock")
Question page & discussion
Up next · Exercise 4Exception Handling Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links