Section5.26Functions and Conditionals Multiple Choice Questions
Checkpoint5.26.1.
Q-1: Which of following uses the correct syntax for a python if statement?
if (x = y):
Try again! An if statement shouldn't contain an assignment operator.
if (x == y):
Correct!
if !(x is not y):
Try again! The not operator (!) can't be used in python.
if x = y:
Try again! An if statement shouldn't contain an assignment operator.
if x == y:
Correct!
Checkpoint5.26.2.
Q-2: Which of the following tests if 5 exponentiated to the 4th power is greater than y and less than or equal to z?
z >= 5**4 > y
Correct! Note that compound statements are allowed in python.
5**4 > y and (5**4 < z or 5**4 == z)
Correct! Note that the and operator comes before the or operator in operator precedence.
5**4 > y and <= z
Try again! Nothing is being compared to z.
5**4 > y and 5**4 < z or 5**4 == z
Try again! The and operator comes before the or operator in operator precedence.
5**4 > y and z >= 5**4
Correct! Note that z >= 5**4 is the same as 5**4 <= z.
Checkpoint5.26.3.
Q-3: Which of the following statements completes the function and returns True if one's age is greater than or equal to 16?
def days_to_age(days):
# what line goes here?
return True
else:
return False
if days / 365 == 16 or 16 < days / 365:
Correct! Note that (16 < days / 365) is the same as (days / 365 > 16).
if (days / 365 = 16) or (16 < days / 365):
Try again! The assignment operator (=) should not be in the if statement.
if days / 365 >= 16:
Correct!
if 365 / days >= 16:
Try again! To convert days to age, you divide days by 365.
if (days / 365 == 16) and (days / 365 > 16):
Try again! One's age can't be 16 and greater than 16.
Checkpoint5.26.4.
Q-4: Which of the following properly expresses the precedence of operators (using parentheses) in the following expression: 10 > y or x + y == 10 and x is not y? Might be best to try on paper first.
(((10 > y) or (x + y == 10)) and (x is not y))
Try again! The precedence of the operators is incorrect.
(10 > (y or x) + y == (10 and x) is not y)
Try again! The precedence of the operators is incorrect.
(10 > y or x + y == 10 and x is not y)
Try again! The precedence of the operators is incorrect.
((10 > y) or (((x + y) == 10) and (x is not y)))
Correct! Parentheses should be added in the following order: (x + y), (x is not y), (10 > y), ((x + y) == 10), (((x + y) == 10) and (x is not y)), ((10 > y) or (((x + y) == 10) and (x is not y))).
(((10 > y) or ((x + y) == 10)) and (x is not y))
Try again! The precedence of the operators is incorrect.
Checkpoint5.26.5.
Q-5: What would be the outputs for transform_string('green'), transform_string('door'), and transform_string('mountains') in order? (Note: Ignore whitespaces.)