Exercise: Python

Questions for: Conditional Statements

Which of the following is used to compare both value and type?
A:
===
B:
is
C:
equals
D:
in
Answer: B
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.
What does the pass statement do in an if block?
A:
Skips the current iteration of a loop
B:
Raises an exception
C:
Terminates the program
D:
Serves as a placeholder for empty code block
Answer: D
The pass statement in an if block is used as a placeholder for an empty code block, ensuring there is no syntax error.
Which keyword is used to exit a loop prematurely?
A:
end
B:
finish
C:
exit
D:
break
Answer: D
The break statement is used to exit a loop prematurely in Python, regardless of the loop's condition.
What is the output of the following code?
value = None
if value:
    print("Value is None")
else:
    print("Value is not None")
A:
Value is None
B:
Value is not None
C:
Error
D:
No output
Answer: B

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".

What is the purpose of the assert statement?
A:
To handle exceptions
B:
To display the current status of the program
C:
To assign None value to an existing object in memory
D:
To check if a condition is true, and if not, raise an AssertionError
Answer: D
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.
Ad Slot (Above Pagination)
Quiz