Lab 8: 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. True or false: When two for loops are nested, their loop variables will both change to the next value from their respective sequences on every iteration of the nested loop.
    True False
  2. If we run the following code, what would the values of the variables a, b, and c be at the end?
    a = 0
    b = 0
    c = 0
    for _ in range(4):
        a += 1
        for _ in range(5):
            b += 1
        c += 1
    print(a, b, c)

    1. a == 4, b == 5, c == 4
    2. a == 4, b == 5, c == 20
    3. a == 4, b == 9, c == 4
    4. a == 4, b == 20, c == 20
    5. a == 9, b == 20, c == 9
    6. other values not listed above
  3. Which of the following problems seems like one where a nested loop would be a good choice as part of a solution? (pick one or more)
    1. Printing out a calendar showing the days in every month of the year.
    2. Asking for input until the user enters a valid value.
    3. Counting how many grid intersections are occupied by one player or the other in a grid-based game like Go or checkers.
    4. Checking for the presence of dangerous items based on separate shipping manifest lists for each train car in a train.
    5. Printing all of the numbers below 1000 that are divisible by 17.

Table of Contents