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.
When slicing or using the
rangefunction, the 'stop' value/number is included in the result.
Which of the following statements are true about index vs. value loops? (pick one or more)
- Index loops are more general, so they're preferable unless a value loop is required.
- Value loops are simpler, so they're preferable unless an index loop is required.
- Which kind of loop you need depends on the length of the sequence.
- Index loops are useful when you need to do something special for the first or last element of a sequence.
- 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.
- 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.
- Index loops don't let you access the values of the sequence, so they're only useful in limited situations.
Which of the following is an index loop over the indices in the word 'hello'? (pick one or more)
for i in 'hello': ...
for i in range('hello'): ...
for i in range(len('hello')): ...
for i in len(range('hello')): ...
for i in range(5): ...
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)
- Check that someone's name, date-of-birth, and account number match against a database.
- Print out the first 20 Fibbonacci numbers.
- Draw lots of little spikes on the tail of a monster.
- Estimate how long someone will have to wait on the phone based on the number of callers already waiting.
- Decide which restaurant is best to recommend based on a user's preferences.
(bonus) Which of these example inputs and outputs is consistent with the following function definition?
(pick one or more)
def abbr(word): result = '' for c in word[:4]: if c not in 'aeiou': result = result + c return result + word[-1]
>>> abbr('hi') 'hi'
>>> abbr('eel') 'll'
>>> abbr('fabulous') 'fbls'
>>> abbr('fabulous') 'fbs'
Table of Contents
- Lab 5 Home
- Part 0: Warm-up with loops
- Part 1: While Loops
- Part 2: For Loop practice
- Part 3: Strings and Loops
- Knowledge Check