Skip to main content

Section 5.27 Functions and Conditionals Multiple Choice Questions

Activity 5.27.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!

Activity 5.27.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.

Activity 5.27.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.

Activity 5.27.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.

Activity 5.27.5.

What would be the outputs for transform_string('green'), transform_string('door'), and transform_string('mountains') in order? (Note: Ignore whitespaces.)
def transform_string(word):
    if len(word) <= 5:
        new_letters = word[1:3].upper()
        if new_letters[0] == new_letters[1]:
            print(new_letters)
        else:
            print("Letters don't match.")
    else:
        new_chars = word[-3:]
        print(new_chars)
    print('Done.')
  • LETTERS DON’T MATCH. Done. oo Done. i Done.
  • Try again! LETTERS DON’T MATCH shouldn’t be all uppercase, oo should be all uppercase, and i is missing characters.
  • EEN. Done. OO Done. ins Done.
  • Try again! EEN is incorrect.
  • Letters don’t match. Done. OO Done. ins Done.
  • Correct!
  • Letters don’t match. OO ins Done.
  • Try again! "Done." is missing twice.
  • RE OO in Done.
  • Try again! RE is incorrect, "Done." is missing twice, and in is missing a character.
You have attempted of activities on this page.