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. A while loop will exit immediately if the continuation condition becomes false, even if it's in the middle of the loop body.
    True False
  2. Which of the following statements are true about while loops?
    1. While loops continue forever until the user asks them to stop.
    2. It's possible for a while loop to iterate 0 times, effectively skipping the loop body.
    3. Within a while loop, you may not create a conditional or another loop.
    4. The continuation condition of a while loop is only evaluated once.
    5. By reading the code for a while loop, the programmer can always figure out how many times the loop will iterate.
  3. Which of the following is an index loop over the indices in the word 'hello'?
    1. i = 0
      while i 
    2. i = 0
      while i 
    3. while i in range(len('hello')):
          letter = 'hello'[i]
          ...
    4. while letter in 'hello':
          ...
  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.

Table of Contents