Skip to main content

Exercises 5.10 Multiple Choice Questions

1.

    What is the result of executing the following code?
    number = 5
    while number <= 5:
        if number < 5:
            number = number + 1
        print(number)
    
  • The program will loop indefinitely
  • Correct! This code loops while number is less than or equal to 5. number only increments if it is less than 5, and it’s originally set to 5, so ’number’ never changes.
  • The value of number will be printed exactly 1 time
  • Incorrect! This would be true if line 3 said "if number <= 5:". Try again.
  • The while loop will never get executed
  • Incorrect! This would be true if number was initialized as a number larger than 5 to start. Try again.
  • The value of number will be printed exactly 5 times
  • Incorrect! This would be true if number was initialized as 0. Try again.

2.

    What will the following code print?
    counter = 1
    sum = 0
    while counter <= 6:
        sum = sum + counter
        counter = counter + 2
    print(sum)
    
  • 12
  • Incorrect! This would be true if counter was initialized as 0. Try again.
  • 9
  • Correct! This loop executes 3 times. After the first loop, sum is 1 and counter is 3. After the second loop, sum is 4 and counter is 5. After the third loop, sum is 9 and counter is 7.
  • 7
  • Incorrect! This is the value of counter, but this code prints the value of sum. Try again.
  • 8
  • Incorrect! This would be the value of counter after the loop if counter was initialized as 0. Try again.

3.

    What will be printed by the following code when it executes?
    sum = 0
    values = [1,3,5,7]
    for number in values:
        sum = sum + number
    print(sum)
    
  • 4
  • Incorrect! This would be true if line 4 said sum = sum + 1. Try again.
  • 0
  • Incorrect! sum increases every iteration of the loop. Try again.
  • 7
  • Incorrect! This would be true if line 4 said sum = number. Try again.
  • 16
  • Correct! This adds up the numbers in values and prints the sum.

4.

    What is the last thing printed when the following code is run?
    number = 0
    while number <= 10:
        print("Number: ", number)
        number = number + 1
    
  • Number: 10
  • Correct! Since this while loop continues while number is less than or equal to 10, the last iteration of the loop will print Number: 10.
  • Number: number
  • Incorrect! Because number is an variable, the print statement will print its value, not its name. Try again.
  • Number: 0
  • Incorrect! Although number is initialized as 0, it increments each iteration. Try again.
  • Number: 11
  • Incorrect! This would be true if number was incremented before each print statement instead of after. Try again.

5.

    What does the following code print?
    output = ""
    x = -5
    while x < 0:
        x = x + 1
        output = output + str(x) + " "
    print(output)
    
  • 5 4 3 2 1
  • Incorrect! x is initialized as -5, not 5. Try again.
  • -4 -3 -2 -1 0
  • Correct! The value of x is incremented before it is printed, so the first value printed is -4.
  • -5 -4 -3 -2 -1
  • Incorrect! x is incremented before it is printed. Try again.
  • This is an infinite loop, so nothing will be printed
  • Incorrect! x increases each loop and will eventually be positive. Try again.

6.

    What are the values of var1 and var2 that are printed when the following code executes?
    output = ""
    var1 = -2
    var2 = 0
    while var1 != 0:
        var1 = var1 + 1
        var2 = var2 - 1
    print("var1:", var1, "var2:" var2)
    
  • var1: -2 var2: 0
  • Incorrect! These are the initial values, but they change during the loop. Try again.
  • var1: 0 var2: -2
  • Correct! This loop will execute two times, so var1 will be 0 and var2 will be -2 when the loop is exited.
  • var1: 0 var2: -1
  • Incorrect! The body of the loop will finish executing before the value of var1 is re-tested. Try again.
  • This is an infinite loop, so nothing will be printed
  • Incorrect! var1 will eventually equal 0, so this loop isn’t infinite. Try again.

7.

    The following code contains an infinite loop. Which is the best explanation for why the loop does not terminate?
    n = 10
    answer = 1
    while n > 0:
        answer = answer + n
        n = n + 1
    print(answer)
    
  • n starts at 10 and is incremented by 1 each time through the loop, so it will always be positive.
  • Correct! The loop will run as long as n is positive. In this case, we can see that n will never become non-positive, so it will run infinitely.
  • answer starts at 1 and is incremented by n each time, so it will always be positive.
  • Incorrect! While it is true that answer will always be positive, answer is not considered in the loop condition. Try again.
  • You cannot compare n to 0 in the while loop. You must compare it to another variable.
  • Incorrect! It is perfectly valid to compare n to 0. Try again.
  • In the while loop body, we must set n to False, and this code does not do that.
  • Incorrect! The loop condition must become False for the loop to terminate, but n by itself is not the condition in this case. Try again.

8.

    Which of the following will add up the numbers from 1 to 4?
    1. for i in range(1,4):
          sum = 0
          sum = sum + i
      
    2. sum = 0
      for i in range(1,4):
          sum = sum + i
      
    3. for i in range(1,5):
          sum = 0
          sum = sum + i
      
    4. sum = 0
      for i in range(1,5):
          sum = sum + sum
      
    5. sum = 0
      for i in range(1,5):
          sum = sum + i
      
  • 1.
  • This will loop from 1 to 3 and reset sum to 0 at the start of each iteration.
  • 2.
  • This will loop from 1 to 3.
  • 3.
  • This will loop from 1 to 4, but will reset the sum to 0 at the start of each iteration.
  • 4.
  • This will loop from 1 to 4, but adds sum to itself.
  • 5.
  • This will loop from 1 to 4 and calculate the sum of those values.