Python From Beginner to Advanced

0% completed

Previous
Next
Quiz
What is a SyntaxError in Python?
A
An error that occurs during the program execution
B
An error that occurs when the program can't understand a file's encoding
C
An error that occurs when Python parser detects a wrong structure
D
An error that occurs due to an incorrect path to the file
What will be the output of the following Python code?
try:
    print("Try block")
    raise ValueError("Raising ValueError")
except ValueError:
    print("Handled ValueError")
finally:
    print("Finally block")
A
Prints "Try block" then "Finally block"
B
Prints "Try block" then "Handled ValueError" then "Finally block"
C
Prints "Handled ValueError" then "Finally block"
D
Error in code
Which statement is true about the finally block in Python error handling?
A
finally block executes regardless of whether an exception was raised or not.
B
finally block executes only if no exceptions are raised in the try block.
C
finally block is used to raise exceptions that occur in the try block.
D
finally block will not execute if the program exits abruptly in a try or except block.
Which of the following is a valid way to raise an exception in Python?
x = -5
if x < 0:
    raise ValueError("Negative value not allowed")
A
The code is incorrect; exceptions cannot be raised using the raise statement.
B
This is correct; it raises a ValueError if x is negative.
C
The code should use except instead of raise to handle negative values.
D
Only system-defined exceptions can be raised, not ValueError.
What does the following code accomplish?
try:
    x = int(input("Enter a positive number: "))
    if x <= 0:
        raise ValueError("That is not a positive number!")
except ValueError as ve:
    print(ve)
A
Prints the input value if it's a positive number
B
Catches and prints any error in the code
C
Raises and catches a ValueError if the input is not a positive number
D
Exits the program if a non-positive number is entered
What does the IndexError exception indicate in Python?
A
An error when a key is not found in a dictionary
B
An error when an index is not found in a sequence, such as a list or tuple
C
An error when incorrect encoding or decoding of a string occurs
D
An error when a mathematical function or operation is not possible
What does the following Python assertion do?
assert (1 == 2), "One is not equal to two."
Choose all correct options
It prints "One is not equal to two."
It does nothing if 1 is equal to 2.
It raises an AssertionError if 1 is not equal to 2.
It checks if 1 is equal to 2 without raising any exceptions.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next