Starting Out with Python 4e (Gaddis)
Chapter 4 Repetition Structures
TRUE/FALSE
1. Reducing duplication of code is one of the advantages of using a loop structure.
ANS: T
2. A good way to repeatedly perform an operation is to write the statements for the task once and then place the statements in a loop that will repeat as many times as necessary.
ANS: T
3. In a flowchart, both the decision structure and the repetition structure use the diamond symbol to represent the condition that is tested.
ANS: T
4. The first line in a while loop is referred to as the condition clause.
ANS: F
5. In Python, an infinite loop usually occurs when the computer accesses an incorrect memory address.
ANS: F
6. Both of the following for clauses would generate the same number of loop iterations.
for num in range(4):
for num in range(1, 5):
ANS: T
7. The integrity of a program’s output is only as good as the integrity of its input. For this reason, the program should discard input that is invalid and prompt the user to enter valid data.
ANS: T
8. Functions can be called from statements in the body of a loop and loops can be called from within the body of a function.
ANS: F
9. In a nested loop, the inner loop goes through all of its iterations for each iteration of the outer loop.
ANS: T
10. To get the total number of iterations in a nested loop, add the number of iterations in the inner loop to the number in the outer loop.
ANS: F
11. A while loop is called a pretest loop because the condition is tested after the loop has had one iteration.
ANS: F
12. In order to draw an octagon with turtle graphics, you would need a loop that iterates eight times.
ANS: T
MULTIPLE CHOICE
1. What type of loop structure repeats the code a specific number of times?
a.
condition-controlled loop
b.
number-controlled loop
c.
count-controlled loop
d.
Boolean-controlled loop
ANS: C
2. What type of loop structure repeats the code based on the value of Boolean expression?
a.
condition-controlled loop
b.
number-controlled loop
c.
count-controlled loop
d.
Boolean-controlled loop
ANS: A
3. What are the values that the variable num contains through the iterations of the following for loop?
for num in range(2, 9, 2):
a.
2, 3, 4, 5, 6, 7, 8, 9
b.
2, 5, 8
c.
2, 4, 6, 8
d.
1, 3, 5, 7, 9
ANS: C
4. What are the values that the variable num contains through the iterations of the following for loop?
for num in range(4):
a.
1, 2, 3, 4
b.
0, 1, 2, 3, 4
c.
1, 2, 3
d.
0, 1, 2, 3
ANS: D
5. Which of the following is not an augmented assignment operator?
a.
*=
b.
/=
c.
+=
d.
<=
ANS: D
6. A variable used to keep a running total is called a(n)
a.
accumulator
b.
total
c.
running total
d.
summer
ANS: A
7. __________ is the process of inspecting data that has been input into a program in order to ensure that the data is valid before it is used in a computation.
a.
Input validation
b.
Correcting data
c.
Data validation
d.
Correcting input
ANS: A
8. A(n) __________ structure is a structure that causes a statement or a set of statements to execute repeatedly.
a.
sequence
b.
decision
c.
module
d.
repetition
ANS: D
9. The first operation is called the __________ and its purpose is to get the first input value that will be tested by the validation loop.
a.
priming read
b.
first input
c.
loop set read
d.
loop validation
ANS: A
10. When will the following loop terminate?
while keep_on_going != 999:
a.
when keep_on_going refers to a value less than 999
b.
when keep_on_going refers to a value greater than 999
c.
when keep_on_going refers to a value equal to 999
d.
when keep_on_going refers to a value not equal to 999
ANS: C
11. In Python, a comma-separated sequence of data items that are enclosed in a set of brackets is called
a.
sequence
b.
variable
c.
value
d.
list
ANS: D
12. In Python, the variable in the for clause is referred to as the __________ because it is the target of an assignment at the beginning of each loop iteration.
a.
target variable
b.
loop variable
c.
for variable
d.
count variable
ANS: A
13. Which of the following represents an example to calculate the sum of numbers (that is, an accumulator), given that the number is stored in the variable number and the total is stored in the variable total?
a.
total + number = total
b.
number += number
c.
total += number
d.
total = number
ANS: C
14. What will be displayed after the following code is executed?
total = 0
for count in range(1,4):
total += count
print(total)
a.
1
3
6
b.
5
c.
1 4
d.
6
ANS: D
15. What will be displayed after the following code is executed?
total = 0
for count in range(4,6):
total += count
print(total)
a.
4
9
b.
4
5
6
c.
4
5
d.
9
ANS: A
16. What will be displayed after the following code is executed?
count = 4
while count < 12:
print("counting")
count = count + 2
a.
counting counting counting counting
b.
counting
counting
counting
counting
c.
counting
counting
d.
counting
counting
counting
ANS: B
17. What will be displayed after the following code is executed?
for num in range(0, 20, 5):
num += num
print(num)
a.
30
b.
25
c.
0 5 10 15 20
d.
5 10 15
ANS: A
18. What does the following program do?
student = 1
while student <= 3:
total = 0
for score in range(1, 4):
score = int(input("Enter test score: "))
total += score
average = total/3
print("Student ", student, "average: ", average)
student += 1
a.
It accepts 4 test scores for 3 students and outputs the average of the 12 scores.
b.
It accepts 3 test scores for each of 3 students and outputs the average for each student.
c.
It accepts 4 test scores for 2 students, then averages and outputs all the scores.
d.
It accepts one test score for each of 3 students and outputs the average of the 3 scores.
ANS: B
COMPLETION
1. A(n) ___________ structure causes a set of statements to execute repeatedly.
ANS: repetition
2. A(n) __________-controlled loop causes a statement or set of statements to repeat as long as the condition is true.
ANS: condition
3. The while loop is known as a(n) __________ loop because it tests the condition before performing an iteration.
ANS: pretest
4. A(n) ___________ loop usually occurs when the programmer does not include code inside the loop that makes the test condition false.
ANS: infinite
5. In Python, you would use the __________ statement to write a count-controlled loop.
ANS: for
6. A(n) __________ total is a sum of numbers that accumulates with each iteration of the loop.
ANS: running
7. A(n) __________ is a special value that marks the end of a sequence of items.
ANS: sentinel
8. The acronym __________ refers to the fact that the computer cannot tell the difference between good data and bad data.
ANS: GIGO
9. A(n) __________ validation loop is sometimes called an error trap or an error handler.
ANS: input
10. The ___________ function is a built-in function that generates a list of integer values.
ANS: range
11. The following for loop iterates ___________ times to draw a square.
for x in range(4):
turtle.forward(200)
turtle.right(90)
ANS: four, 4