try:
x = int("42")
except ValueError:
x = "Invalid conversion"
else:
x += 1
finally:
x *= 2
What will be the value of "x" after the execution of this code?
A:
42
B:
"42"
C:
"Invalid conversion"
D:
86
Answer:D
The try block successfully converts the string "42" to an integer. The else block increments the value of "x" by 1, and the finally block multiplies "x" by 2.
Discuss About this Question.
Which of the following statements is true about the raise statement?
A:
It is used to terminate the program
B:
It can only raise built-in exceptions
C:
It is used to catch exceptions
D:
It is used to explicitly raise an exception
Answer:D
The raise statement is used to explicitly raise an exception in Python.
Discuss About this Question.
Consider the following Python code:
try:
value = int("abc")
except ValueError:
result = "Invalid conversion"
finally:
result += " - Finally block executed"
What will be the value of "result" after the execution of this code?
A:
"Invalid conversion"
B:
"Invalid conversion - Finally block executed"
C:
" - Finally block executed"
D:
Raises an exception
Answer:B
The finally block is executed regardless of whether an exception occurs or not. It appends " - Finally block executed" to the "result" string.
Discuss About this Question.
In Python, what is the purpose of the except block without specifying an exception type?
A:
It catches all exceptions
B:
It is a syntax error
C:
It only catches specific exceptions
D:
It is not allowed
Answer:A
An except block without specifying an exception type catches all exceptions, which can be useful for handling unexpected errors.
Discuss About this Question.
Which built-in Python function is used to raise an exception?
A:
raise()
B:
throw()
C:
except()
D:
error()
Answer:A
The raise() function is used to explicitly raise an exception in Python.
Discuss About this Question.
Ad Slot (Above Pagination)
Install ExamAdept
Fast access — add this app to your device.
To install on iPhone/iPad: tap Share → Add to Home Screen.
Discuss About this Question.