Python Programming - Conditional Statements

Exercise : Conditional Statements - General Questions
  • Conditional Statements - General Questions
16.
Which keyword is used to exit a loop prematurely?
end
finish
exit
break
Answer: Option
Explanation:
The break statement is used to exit a loop prematurely in Python, regardless of the loop's condition.

17.
What does the pass statement do in an if block?
Skips the current iteration of a loop
Raises an exception
Terminates the program
Serves as a placeholder for empty code block
Answer: Option
Explanation:
The pass statement in an if block is used as a placeholder for an empty code block, ensuring there is no syntax error.

18.
Which of the following is used to compare both value and type?
===
is
equals
in
Answer: Option
Explanation:
The is keyword is used for object identity, checking if two variables refer to the same object in memory. It compares both value and type.

19.
What is the purpose of the finally block in a try-except-finally statement?
To handle exceptions
To define the main body of the code
To always execute code, whether an exception occurs or not
To terminate the program
Answer: Option
Explanation:

The finally block is used to define code that will be executed no matter what, whether an exception occurs in the try block or not.

This block is typically used for cleanup operations, such as closing files or releasing resources, that should be executed regardless of whether an exception was raised within the try block or not.

It ensures that certain actions are performed even if an exception occurs and is caught by an except block, or if no exception occurs at all.


20.
Which of the following statements is used to raise a custom exception?
assert
throw
raise
custom
Answer: Option
Explanation:
The raise statement is used to raise a custom exception in Python. You can use it to trigger a specific exception with a custom error message.