Lab 5: Knowledge Check

These questions check your knowledge of concepts from this lab. Use the button at the bottom to show your answers once you've attempted them.

  1. When slicing or using the range function, the 'stop' value/number is included in the result.
    True False
  2. Which of the following statements are true about index vs. value loops? (pick one or more)
    1. Index loops are more general, so they're preferable unless a value loop is required.
    2. Value loops are simpler, so they're preferable unless an index loop is required.
    3. Which kind of loop you need depends on the length of the sequence.
    4. Index loops are useful when you need to do something special for the first or last element of a sequence.
    5. In a given iteration, value loops don't allow you to access the next item in a sequence, so you would need an index loop for that.
    6. In a given iteration, value loops don't allow you to access the previous item in a sequence, so you would need an index loop for that.
    7. Index loops don't let you access the values of the sequence, so they're only useful in limited situations.
  3. Which of the following is an index loop over the indices in the word 'hello'? (pick one or more)
    1. for i in 'hello':
          ...
    2. for i in range('hello'):
          ...
    3. for i in range(len('hello')):
          ...
    4. for i in len(range('hello')):
          ...
    5. for i in range(5):
          ...
  4. For which of the following tasks would a loop be an appropriate tool to use as part of an elegant solution? (pick one or more)
    1. Check that someone's name, date-of-birth, and account number match against a database.
    2. Print out the first 20 Fibbonacci numbers.
    3. Draw lots of little spikes on the tail of a monster.
    4. Estimate how long someone will have to wait on the phone based on the number of callers already waiting.
    5. Decide which restaurant is best to recommend based on a user's preferences.
  5. (bonus) Which of these example inputs and outputs is consistent with the following function definition?
    def abbr(word):
        result = ''
        for c in word[:4]:
            if c not in 'aeiou':
                result = result + c
        return result + word[-1]
    (pick one or more)
    1. >>> abbr('hi')
      'hi'
    2. >>> abbr('eel')
      'll'
    3. >>> abbr('fabulous')
      'fbls'
    4. >>> abbr('fabulous')
      'fbs'

Table of Contents