What happens when this Python code runs?
temperature = 72
if temperature > 80:
print("It is hot.")
Use this free practice quiz with 20 questions to review 3 Conditional Control Flow, test your knowledge, and prepare for your next test or exam.
What happens when this Python code runs?
temperature = 72
if temperature > 80:
print("It is hot.")
It prints It is hot.
It prints nothing
It raises an error because 72 is too low
It changes temperature to 80
What does this code print?
number = 0
if number == 0:
print("Zero")
elif number >= 0:
print("Positive")
else:
print("Negative")
Positive
Negative
Zero
Nothing
Given items = ["pen", "notebook"], what is the result of the condition items and items[0] == "pen" in an if statement?
The condition is true
The condition is false
Python raises an IndexError before comparing the first item
Python skips the entire condition because and always short-circuits
Which two values below are treated as false in a Python conditional context? Select all that apply.
False
"hello"
None
[1]
42
Which three statements accurately describe the logical expressions used in Python conditional control flow? Select all that apply.
age >= 18 and has_id requires both conditions to be true
is_weekend or is_holiday can be true when either condition is true
not is_locked has the same truth value as is_locked
member and (balance > 0 or balance == 0) can be true when member is true and the balance is nonnegative
True or false: In Python, an elif clause is tested only after the preceding condition is false.
True
False
True or false: Python requires every conditional block to be indented by exactly four spaces, even when a different consistent indentation level is used throughout the block.
True
False
What kind of operation does Python perform when it evaluates score == 100?
According to the material, how many spaces does Python conventionally use for each indentation level?
Complete the description: Use to test an additional condition, and use for a default action when no preceding condition is true.
Complete the sentence: A nonempty string is generally treated as in a Python conditional context.
Predict the output of this code and explain why the later branches do not execute:
score = 84
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "Needs improvement"
print(grade)
What does this code print?
age = 21
has_ticket = False
if age >= 18:
if has_ticket:
print("You may enter")
else:
print("A ticket is required")
else:
print("You must be at least 18")
You must be at least 18
A ticket is required
You may enter
Nothing
For age = 25, what is the result of the chained comparison 18 <= age < 65?
The condition is true
The condition is false
Python interprets it as (18 <= age) < 65, causing a type error
Python does not allow chained comparisons
Which Python keyword introduces an additional condition that is tested only if the preceding if condition is false?
Which condition follows the recommended Python practice for checking whether the variable value is the singleton None object?
value == None
value is None
value = None
value in None
A program should reject a password only when it appears in the list revoked_passwords. Which condition correctly identifies a revoked password?
if password in revoked_passwords:
if password not in revoked_passwords:
if password != revoked_passwords:
if password is not revoked_passwords:
Which implementation correctly returns "Order is empty" for an empty order, "Payment is required" for an unpaid order, and "Order processed" otherwise while keeping the successful path minimally nested?
def process_order(order):
if order:
if order.is_paid:
return "Order processed"
return "Payment is required"
return "Order is empty"
def process_order(order):
if not order:
return "Order processed"
if not order.is_paid:
return "Payment is required"
return "Order is empty"
def process_order(order):
if not order:
return "Order is empty"
if not order.is_paid:
return "Payment is required"
return "Order processed"
def process_order(order):
if not order.is_paid:
return "Payment is required"
if not order:
return "Order is empty"
return "Order processed"
If name = "Ada" and names = ["Grace", "Linus"], the condition name not in names evaluates to True.
True
False
In a Python function that uses early validation, which keyword exits the function immediately after an invalid input is detected?