What does this valid Python code print?
age = 16
if age >= 18:
print("Adult")
else:
print("Minor")
Use this free practice quiz with 20 questions to review 04. Conditional Statements, test your knowledge, and prepare for your next test or exam.
What does this valid Python code print?
age = 16
if age >= 18:
print("Adult")
else:
print("Minor")
It prints "Minor".
It prints "Adult".
It prints nothing.
It raises a syntax error.
What value is assigned to grade when score = 85 in this valid Python chain?
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
else:
grade = "F"
A
B
C
F
What message is printed by this valid Python nested conditional when username = "" and password_correct = True?
if username != "":
if password_correct:
print("Login successful")
else:
print("Incorrect password")
else:
print("Username is required")
Login successful
Incorrect password
Username is required
No output
Select all statements that are correct about Python Boolean expressions.
True and False evaluates to False.
True or False evaluates to False.
not False evaluates to True.
False and False evaluates to True.
Which practices belong to the validation sequence described in the material? Select all that apply.
Check whether the input has the correct basic form.
Check whether the value is within the allowed range.
Always perform the dependent operation before validation.
Report an error or reject the input when a rule fails.
Continue only when the input is valid.
True or false: In Python, a single = is the correct operator for comparing two values in an if condition.
True
False
True or false: In the material's classify_speed example, a speed of 30 is classified as Slow, while a speed of 31 is classified as Moderate.
True
False
Which Python keyword introduces the alternative branch in a two-way if-else decision?
Enter the Python keyword that means “else if” and adds another condition to a chained conditional.
Complete the statement: In Python, the punctuation mark after an if condition marks the beginning of the indented block.
Complete the statement: Inside a function, a guard clause often uses to handle an invalid or exceptional case immediately.
Explain the recommended validation sequence for conditional logic and why an operation such as division should occur only after its relevant invalid case has been checked.
What does classify_speed(60) return in the material's range-classification function?
Fast
Moderate
Slow
Invalid
True or false: In an if/elif chain, Python executes every branch whose condition is true.
True
False
In the validation pattern that handles an invalid or exceptional case immediately so the main operation is less deeply nested, what two-word pattern name is used?
What is printed by this Python code?
items = []
if items:
print("Items found")
else:
print("No items")
Items found
No items
The list itself
An error occurs
What value is assigned to status?
age = 16
status = "adult" if age >= 18 else "minor"
minor
adult
16
An error occurs
What is the value of message after this code runs?
value = None
if value:
message = "present"
else:
message = "missing"
present
None
missing
The variable is unchanged
A program should accept either "yes" or "y" as the value of choice. Which condition correctly tests both possibilities?
if choice == "yes" or "y":
if choice == ("yes" or "y"):
if choice in "yes", "y":
if choice == "yes" or choice == "y":
A program sets records = [] and then evaluates if records:. What Python Boolean value does the condition have?