Python Programming - Conditional Statements

Exercise : Conditional Statements - General Questions
  • Conditional Statements - General Questions
11.
Which of the following statements is used for handling exceptions?
try-case
for-in
while-break
try-except
Answer: Option
Explanation:
The try-except statement is used for exception handling in Python. Code within the try block is executed, and if an exception occurs, it can be caught and handled in the except block.
try:
    # code that might raise an exception
except SomeException:
    # code to handle the exception

12.
What does the continue statement do in a loop?
Exits the loop
Skips the remaining code in the loop and moves to the next iteration
Repeats the loop indefinitely
Skips the remaining code and exits the loop
Answer: Option
Explanation:
The continue statement is used to skip the remaining code in the current iteration of a loop and move on to the next iteration.

13.
How can you combine multiple conditions using the logical OR operator?
||
or
&&
and
Answer: Option
Explanation:
The or keyword is used as the logical OR operator to combine multiple conditions in an if statement. At least one of the conditions must be true for the combined condition to be true.

14.
What is the purpose of the assert statement?
To handle exceptions
To display the current status of the program
To assign None value to an existing object in memory
To check if a condition is true, and if not, raise an AssertionError
Answer: Option
Explanation:
The assert statement is used to test if a given condition is true, and if not, it raises an AssertionError with an optional error message.

15.
What is the output of the following code?
value = None
if value:
    print("Value is None")
else:
    print("Value is not None")
Value is None
Value is not None
Error
No output
Answer: Option
Explanation:

In this code, the condition if value: checks whether value evaluates to True. Since value is None, which is considered falsy in Python, the condition evaluates to False.

Therefore, the code block under the else statement gets executed, printing "Value is not None".