6.9. Multiple Choice Questions¶
- 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.
Q-1: What is the result of executing the following code?
number = 5
while number <= 5:
if number < 5:
number = number + 1
print(number)
- 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.
Q-2: What will the following code print?
counter = 1
sum = 0
while counter <= 6:
sum = sum + counter
counter = counter + 2
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.
Q-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)
- 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.
Q-4: What is the last thing printed when the following code is run?
number = 0
while number <= 10:
print("Number: ", number)
number = number + 1
- 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.
Q-5: What does the following code print?
output = ""
x = -5
while x < 0:
x = x + 1
output = output + str(x) + " "
print(output)
- 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.
Q-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: " + str(var1) + " var2 " + str(var2))
- 0
- Incorrect! Each loop will iterate as many times as there are elements in its list. Try again.
- 4
- Incorrect! The print statement is inside of both loops. Try again.
- 5
- Incorrect! The print statement is inside of both loops. Try again.
- 20
- Correct! The outer loop will iterate 4 times and the inner loop will iterate 5 times. 4 times 5 = 20.
- This is an infinite loop, so nothing will be printed
- Incorrect! This loop is not infinite. Try again.
Q-7: How many asterisks will be printed when the following code executes?
for x in [0, 1, 2, 3]:
for y in [0, 1, 2, 3, 4]:
print('*')
- 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.
Q-8: 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)
- 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.
Q-9: 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.
- Current Letter : P
- Incorrect! This will be printed. Try again.
- Current Letter : t
- Incorrect! This will be printed. Try again.
- Current Letter : h
- Correct! Because continue sends the loop to the next iteration at h, it will not print "Current Letter: h".
- Current Letter : o
- Incorrect! This will be printed. Try again.
Q-10: Which of the following statements won’t be printed when this Python code is run?
for letter in 'Python':
if letter == 'h':
continue
print('Current Letter : ' + letter)
- walking
- Not all of the characters will be added to the output string.
- wlking
- This would be true if it didn't break when it found an 'i'
- wlk
- It will not add the 'a' and will stop when it reaches the 'i'
- wlkng
- This would be true if when it found an 'i' it did a continue rather than a break
Q-11: What will the following code print?
def mystery(str):
out = ""
for char in str:
if char == "i":
break
if char == 'a':
continue
out += char
return out
print(mystery("walking"))
- 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.
Q-12: Which of the following will add up the numbers from 1 to 4?
for i in range(1,4):
sum = 0
sum = sum + i
sum = 0
for i in range(1,4):
sum = sum + i
for i in range(1,5):
sum = 0
sum = sum + i
sum = 0
for i in range(1,5):
sum = sum + sum
sum = 0
for i in range(1,5):
sum = sum + i
- [3, 1]
- This adds any non negative values to the out list.
- [3, -2, 1]
- This will not add any negative values to the list.
- [3]
- This would be true if the continue was a break
- [3, -3, -2, 1]
- This would be true if it just added all values to out
Q-13: What will the following code print?
def mystery(nums):
out = []
for num in nums:
if num < 0:
continue
else:
out.append(num)
return out
print(mystery([3, -3, -2, 1]))
- 1.
- This will loop 5 times (0 to 4) and print 0 "*" on the first row, 1 on the second, etc.
- 2.
- This will loop 5 times (0 to 4) and print five "*" on each row.
- 3.
- This will loop 4 times (1 to 4) and print 0 "*" on the first row, 1 on the second, etc.
- 4.
- This will loop 4 times (1 to 4) and print five "*" on each row.
Q-14: Which of the following will print five rows with five ‘*’ in each row?
for i in range(0,5):
print("*" * i)
for i in range(0,5):
print("*" * 5)
for i range(1,5):
print("*" * i)
for i in range(1,5):
print("*" * 5)
- 1 1 2 2 3 3
- It prints both i and j each time through the loop.
- 1 2 3 1 2 3 1 2 3
- This would be true if it only printed j each time through the loop.
- 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3
- It prints both i and j each time through the loop. The value of i starts at 1 and j changes from 1 to 3 before i changes.
- 1 1 2 1 3 1 2 1 2 2 2 3 3 1 3 2 3 3
- Remember that i starts at 1 and j will change from 1 to 3 before i changes.
Q-15: What will the following code print?
for i in range(1,4):
for j in range(1,4):
print(i, j, end=' ')
- 1018
- This would be true if it added all values in the list to total
- 1009
- This would be true if it added the 999 to the list before the break
- 19
- This would be true if it used continue rather than break when it found 999
- 10
- This will return the total of the values in the list before the 999
Q-16: What will the following code print?
def mystery(nums):
total = 0
for num in nums:
if num == 999:
break
else:
total += num
return total
mystery([8, 2, 999, 5, 4])
print(mystery("walking"))