Skip to main content

Section 2.11 Unit 2 Loops Multiple-Choice Exercises

These are exercises mostly written by Dr. Barb Ericson in the Python for Everybody Interactive ebook on Runestone.

Activity 2.11.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.

Activity 2.11.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 = 1 and counter = 3, after the second loop sum = 4 and counter = 5, and after the third loop sum = 9 and counter = 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.

Activity 2.11.3.

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.

Activity 2.11.4.

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.

Activity 2.11.5.

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: " + str(var1) + " var2 " + str(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.

Activity 2.11.6.

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.

Activity 2.11.7.

Which type of loop can be used to perform the following iteration: You choose a positive integer at random and then print the numbers from 1 up to and including the selected integer.
  • a for-loop or a while-loop
  • Correct! Although you do not know how many iterations you loop will run before the program starts running, once you have chosen your random integer, Python knows exactly how many iterations the loop will run, so either a for-loop or a while-loop will work.
  • only a for-loop
  • Incorrect! As you learned in section 7.2, a while-loop can always be used for anything a for-loop can be used for. Try again.
  • only a while-loop
  • Incorrect! Although you do not know how many iterations you loop will run before the program starts running, once you have chosen your random integer, Python knows exactly how many iterations the loop will run, so this is an example of definite iteration. Try again.

Activity 2.11.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.

Activity 2.11.9.

What does the following code print?
x = 6
while(x > 4)
 print(x, end=' ')
 x = x - 1
  • 6 5
  • Correct! Each time the loop runs, value of x decrements by 1. So, when its value gets down to 4, the loop condition is no longer satisfied.
  • 6 5 4
  • Incorrect! Each time the loop runs, value of x decrements by 1. So, when its value gets down to 4, the loop condition is no longer satisfied.
  • 6 5 4 3
  • Incorrect! Each time the loop runs, value of x decrements by 1. So, when its value gets down to 4, the loop condition is no longer satisfied.
  • 5 4 3
  • Incorrect! Each time the loop runs, value of x decrements by 1. So, when its value gets down to 4, the loop condition is no longer satisfied.

Activity 2.11.10.

What does the following code print?
i=0

while(i < 3)
     print(i, end=' ')
  • 0 0 0
  • Incorrect! The value of i never changes from 0. So, the loop condition is always true and it will keep printing i=0.
  • 0 1 2
  • Incorrect! The value of i never changes from 0. So, the loop condition is always true and it will keep printing i=0.
  • 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 . . . .
  • Correct! The value of i never changes from 0. So, the loop condition is always true and it will keep printing i=0.
  • 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 . . . .
  • Incorrect! The value of i never changes from 0. So, the loop condition is always true and it will keep printing i=0.

Activity 2.11.11.

Which of the following correctly translates the for loop below to a while loop?
for i in range(n):
<body>
  • i = 0
    while(i < n)
       <body>
    
  • Incorrect! This will be an infinite loop as the value of i never changes.
  • i = 0
    while(i < n)
       <body>
       i = i + 1
    
  • Correct! The value of i increments by 1 in each iteration till it becomes equal to n at which point the loop condition won’t be satisfied.
  • i = 0
    while(i < n)
       <body>
       n = n + 1
    
  • Incorrect! This is not the right implementation of the given for loop as the value of i remains the same and the value of n keeps increasing with each iteration.
  • i = 1
    while(i < n)
       <body>
       i = i + 1
    
  • Incorrect! This is not the right implementation of the given for loop as the value of i remains the same and the value of n keeps increasing with each iteration.
You have attempted of activities on this page.