# Exam Questions for Chapters 7 and 8¶

The following questions test what you have learned in chapters 7 and 8. Click the “Start” button when you are ready to begin the exam. Click the “Pause” button to pause the exam (you will not be able to see the questions when the exam is paused). It will show how much time you have used, but you have unlimited time. Click on the “Finish Exam” button at the end when you are done. The number correct, number wrong, and number skipped will be displayed at the bottom of the page. Feedback for each answer will also be shown as well as your answer.

You will not be able to change your answers after you hit the “Finish Exam” button.

8-8-1: 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
• Since this while loop continues while number is less than or equal to 10 the last time in the loop it will print Number: 10.
• Number: number
• This would be true if it was print ("Number: ", "number"). But since there are no quotes around number it will print the value of number.
• Number: 0
• While number is set to 0 to start it increments each time inside the loop.
• Number: 11
• This would be true if the print statement was after number was incremented by 1, but it is before.

8-8-2: When the following code is run, how many times is hello printed?

helloArray = range(1,4)
for x in helloArray:
print ("hello")

• 1
• This would be true if the print was outside of the loop, but it is in the loop.
• 2
• This would be true if it was range(1,3)
• 3
• The range(1,4) returns a list with the values 1, 2, and 3. So this will print hello 3 times.
• 4
• This would be true if it was range(1,5). Remember that it includes the first value and ends before the second value.

8-8-3: 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
• This code loops while number is less than or equal to 5 and number only increments if it is less than 5 and it is originally set to 5 so number never changes.
• The value of number will be printed exactly 1 time
• This would be true if it was if number <= 5.
• The while loop will never get executed
• This would be true if number was set to a number larger than 5 to start.
• The value of number will be printed exactly 5 times
• This would be true if number was set to 1 to start.

8-8-4: What will be printed by the following code when it executes?

from turtle import *
sum = 0
values = [1,3,5,7]
for number in values:
sum = sum + number
print (sum)

• 4
• This would be true if it was sum = sum + 1
• 0
• This would be true if sum never changed, but each time through the loop number is added to the current sum.
• 7
• This would be true if it printed the number.
• 16
• This adds up the numbers in values and prints the sum.

8-8-5: What will the following code print?

counter = 1
sum = 0
while counter <= 6:
sum = sum + counter
counter = counter + 2
print (sum)

• 12
• This would be true if counter started off with a value of 0.
• 9
• 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
• This is the value of counter, but this code prints the value of sum.
• 8
• This would be the value of counter after the loop if counter started at 0.
Next Section - Chapter 9 - Repeating Steps with Strings