[go: up one dir, main page]

0% found this document useful (0 votes)
16 views9 pages

Final-Exception More

The document contains a series of questions and answers related to exception handling in Python, covering topics such as try-except blocks, raising exceptions, and the behavior of finally clauses. Each question presents a code snippet or scenario, followed by multiple-choice answers. The content is designed to test knowledge and understanding of error handling mechanisms in Python programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views9 pages

Final-Exception More

The document contains a series of questions and answers related to exception handling in Python, covering topics such as try-except blocks, raising exceptions, and the behavior of finally clauses. Each question presents a code snippet or scenario, followed by multiple-choice answers. The content is designed to test knowledge and understanding of error handling mechanisms in Python programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

1. What will be the output of the following code?

python
Copy
Edit
try:
num = int("abc")
except ValueError:
print("Conversion failed")
else:
print("Conversion succeeded")
A) Conversion failed
B) Conversion succeeded
C) No output
D) Runtime error

2. How can you modify this function to raise an exception if the input is negative?
python
Copy
Edit
def check_positive(n):
# add code here
return n
A)

python
Copy
Edit
if n < 0:
raise ValueError("Negative number")
B)

python
Copy
Edit
if n < 0:
print("Negative number")
C)

python
Copy
Edit
if n > 0:
raise ValueError("Negative number")
D)

python
Copy
Edit
if n < 0:
return ValueError("Negative number")
3. What is the output of this snippet?
python
Copy
Edit
try:
print("Start")
raise KeyError("Missing key")
except KeyError as e:
print("Caught:", e)
finally:
print("Cleanup")
A) Start
Cleanup
B) Start
Caught: Missing key
Cleanup
C) Caught: Missing key
D) Cleanup

4. Which of these is the correct way to catch either IndexError or KeyError in a


single except block?
A)

python
Copy
Edit
except IndexError, KeyError:
B)

python
Copy
Edit
except (IndexError, KeyError) as e:
C)

python
Copy
Edit
except IndexError | KeyError as e:
D)

python
Copy
Edit
except IndexError and KeyError as e:
5. What will the following function print when called?
python
Copy
Edit
def foo():
try:
return 1
finally:
return 2

print(foo())
A) 1
B) 2
C) None
D) Raises an error

6. What will happen if the finally block contains a return statement, and the try
block also returns a value?
A) The try block�s return value is returned.
B) The finally block�s return value overrides the try return value.
C) An exception is raised.
D) Both values are returned as a tuple.
7. What does the following code print?
python
Copy
Edit
try:
raise RuntimeError("fail")
except Exception:
print("Exception caught")
else:
print("No Exception")
finally:
print("Always executed")
A) Exception caught
Always executed
B) No Exception
Always executed
C) Always executed
D) Exception caught

8. Which of these snippets correctly re-raises the caught exception?


A)

python
Copy
Edit
except Exception as e:
raise e
B)

python
Copy
Edit
except Exception:
raise
C) Both A and B
D) Neither A nor B

9. What happens if an exception is raised in the except block and there is a


finally block?
A) The new exception replaces the original and finally block executes.
B) The original exception is re-raised and finally block is skipped.
C) The new exception is ignored and execution continues.
D) A syntax error occurs.

10. What will this code output?


python
Copy
Edit
try:
1 / 0
except ZeroDivisionError:
print("Zero division!")
except Exception:
print("General exception")
A) Zero division!
B) General exception
C) No output
D) Runtime error
11. How do you raise a custom exception MyError with the message "Something went
wrong"?
A)

python
Copy
Edit
raise MyError("Something went wrong")
B)

python
Copy
Edit
throw MyError("Something went wrong")
C)

python
Copy
Edit
raise("MyError", "Something went wrong")
D)

python
Copy
Edit
raise Exception("MyError", "Something went wrong")
12. What will be printed by the code below?
python
Copy
Edit
def func():
try:
x = 1 / 0
except ZeroDivisionError:
print("Error handled")
finally:
print("Cleanup")

func()
A) Error handled
Cleanup
B) Cleanup
C) Runtime error
D) No output

13. If you want to ensure some code always runs after a try block�whether or not an
exception was raised�you should place it in:
A) The else block
B) The except block
C) The finally block
D) The try block

14. What will happen if a try block raises an exception, and there are no except
blocks but a finally block is present?
A) The exception is ignored and the finally block runs.
B) The exception propagates after the finally block executes.
C) The program terminates before finally runs.
D) The code causes a syntax error.
15. What�s the output of this code?
python
Copy
Edit
try:
print("Try block")
raise ValueError("Oops")
except TypeError:
print("Type error caught")
else:
print("Else block")
finally:
print("Finally block")
A) Try block
Else block
Finally block
B) Try block
Finally block
C) Try block
Type error caught
Finally block
D) Try block
Finally block
Type error caught

16. How can you catch all exceptions except system-exiting exceptions (SystemExit,
KeyboardInterrupt)?
A) Use except Exception:
B) Use except BaseException:
C) Use except: without specifying any exception
D) Catch SystemExit and KeyboardInterrupt explicitly

17. What does this code print?


python
Copy
Edit
try:
raise IOError("IO error")
except (ValueError, IOError) as e:
print("Caught:", type(e).__name__)
A) ValueError
B) IOError
C) Exception
D) No output

18. Given the function below, what happens when f() is called?
python
Copy
Edit
def f():
try:
raise Exception("error")
except Exception as e:
print("Handled", e)
raise

try:
f()
except Exception:
print("Caught in outer try")
A) Prints "Handled error" then "Caught in outer try"
B) Only prints "Handled error"
C) Only prints "Caught in outer try"
D) Raises uncaught exception and terminates

19. What�s the output of this code?


python
Copy
Edit
try:
x = [1, 2, 3]
print(x[3])
except IndexError:
print("Index error")
else:
print("No error")
finally:
print("Done")
A) Index error
Done
B) No error
Done
C) Done
D) Index error

20. How do you catch multiple exception types and handle them separately?
A) Use multiple except blocks, each for one exception type
B) Use a single except block with a tuple of exceptions
C) Use if statements inside except block to differentiate exception types
D) Both A and C

21. What will the following code print?


python
Copy
Edit
def divide(a, b):
try:
return a / b
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Executing finally")

print(divide(10, 0))
A) Cannot divide by zero
Executing finally
None
B) Cannot divide by zero
Executing finally
Runtime error
C) Executing finally
None
D) Runtime error

22. How to properly write an except block that handles an exception and logs the
error message?
A)
python
Copy
Edit
except Exception as e:
print("Error:", e)
B)

python
Copy
Edit
except Exception:
print("Error")
C)

python
Copy
Edit
except:
print("Error:", e)
D)

python
Copy
Edit
except Exception as e:
raise e
23. Which keyword is used to throw an exception in Python?
A) throw
B) raise
C) error
D) catch

24. What happens if an exception occurs in a function but is not caught inside the
function?
A) The exception is silently ignored.
B) The exception propagates to the caller.
C) The program terminates immediately.
D) The function returns None.

25. What will be the output of the code below?


python
Copy
Edit
try:
print("A")
raise ValueError("error")
except ValueError as e:
print(e)
else:
print("No error")
finally:
print("Cleanup")
A) A
error
Cleanup
B) A
No error
Cleanup
C) error
Cleanup
D) A
Cleanup

26. Which code snippet will ensure cleanup code runs even if there�s a return
statement inside the try block?
A)

python
Copy
Edit
try:
return 10
finally:
print("Cleanup")
B)

python
Copy
Edit
try:
return 10
except:
print("Error")
finally:
print("Cleanup")
C)

python
Copy
Edit
try:
print("Doing work")
finally:
print("Cleanup")
D) All of the above

27. What will this code output?


python
Copy
Edit
try:
x = 1 / 0
except ZeroDivisionError as e:
print("Error:", e)
raise
A) Error: division by zero
Program terminates with exception
B) Error: division by zero
Program continues normally
C) No output
D) Syntax error

28. Can try and finally be used without except?


A) Yes, to guarantee cleanup code runs even if no exception is caught
B) No, except is required after try
C) Yes, but only in Python 2.x
D) No, causes syntax error
29. What�s the effect of this code?
python
Copy
Edit
try:
x = int("a")
except ValueError as e:
print("ValueError:", e)
raise
A) Prints error message and terminates with exception
B) Prints error message and continues execution normally
C) Only raises exception without printing
D) Silently ignores exception

30. What will happen here?


python
Copy
Edit
try:
x = 5
finally:
print("Cleanup")
A) Prints Cleanup and program exits normally
B) Syntax error (no except block)
C) Cleanup prints only if exception raised
D) Program crashes

You might also like