LMU ☀️ CMSI 1010
COMPUTER PROGRAMMING LABORATORY
Quiz 2

Options with circles are single-select (exactly one), while options with squares are multi-select (zero or more).

  1. If we have a file called food.py with functions cook and bake, which of the following are true?
    • You can say from food import cook, bake and then later call these functions like food.cook() and food.bake()
    • You can say import food and then later call these functions like food.cook() and food.bake()
    • You can say import food and then later call these functions like cook() and bake()
    • You can say from food import cook, bake and then later call these functions like cook() and bake()
  2. Which of the following are problems with the following code:
        def is_even():
            if n % 2 == 0:
                return True
    
    • The parameter is missing in the declaration
    • There is an extra colon after 0
    • is_even should be isEven
    • It never returns for non-even parameters
    • == should be =
    • True should be true
  3. If x is 3.141592653589793238, what is the string f"{x:10.5f}"?
  4. Complete the match statement below to increments x by 1 if the value of the variable direction is "east", decrements x by 1 if the value of this variable is "west", increments y by 1 if it is "north", decrements y by 1 if it is "south", and raises a ValueError if it is anything else.
    match direction:
        case "east":
    
        case "west":
    
        case "north":
    
        case "south":
    
        case _:
    
    
  5. Given a dictionary called capitals with contents such as {"CA": "Sacramento", "HI": "Honolulu", ...}, how would you select a random state?
    • random.choice(list(capitals.keys()))
    • random.choice(capitals)
    • capitals[2]
    • random_state()
  6. Given ages = {"Yoko": 8, "Diego": 3, "Julian": 8, "Kimberly": 5}, what are:
        len(ages)            _____________________________
        ages["Diego"]        _____________________________
        list(ages.keys())    _____________________________
        list(ages.values())  _____________________________
        "Yoko" in ages       _____________________________
    
  7. If x = 39958302.3950, what are: locale.currency(x) and locale.currency(x, grouping=True) after locale.setlocale(locale.LC_ALL, 'en_US') has been called?
    • "$39958302.39" and "$39,958,302.39"
    • "$40000000" and "$40,000,000"
    • "39958302.39 $" and "39,958,302.39 $"
    • "$39958302.40" and "$39,958,302.40"
    • "39958302.40 $" and "39,958,302.40 $"