Gaddis Python 2e Chapter 05
Download file with the answers
Gaddis Python 2e Chapter 05
1 file(s) 163.48 KB
If you are not a member register here to download this file
Chapter Five
MULTIPLE CHOICE
1. What is the disadvantage of coding in one long sequence structure?
a. Duplicated code makes the program faster to write.
b. Writing a long sequence of statements is error prone.
c. If parts of the duplicated code have to be corrected, the correction has to be made many times.
d. It does not make use of decision structures.
2. 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
3. What type of loop structure repeats the code based on the value of the Boolean expression?
a. Condition-controlled loop
b. Number-controlled loop
c. Count-controlled loop
d. Boolean-controlled loop
4. What is the format for the while clause in Python?
a. while condition
b. while condition :
c. while condition statement
d. while condition : statement
5. 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
6. 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
7. The variable used to keep the running total is called a(n) _____.
a. Accumulator
b. Total
c. running total
d. grand total
8. What is not an example of an augmented assignment operator?
a. *=
b. /=
c. -=
d. <=
9. _____ is the process of inspecting data that has been input to a program to make sure it is valid before it is used in a computation.
a. Input validation
b. Correcting data
c. Data validation
d. Correcting input
10. The first input 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
11. What is the structure that causes a statement or a set of statements to execute repeatedly?
a. Sequence
b. Decision
c. Module
d. Repetition
12. 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
13. In Python, a comma-separated sequence of data items that are enclosed in a set of brackets is called a _____.
a. sequence
b. variable
c. value
d. list
14. 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
15. Which of the following represents an example to calculate the sum of the numbers (accumulator)?
a. total + number = total
b. number += number
c. total += number
d. total = number
TRUE/FALSE
1. True/False: Reducing duplication of code is one of the advantages of using a loop structure.
ANS: T
2. True/False: A better 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 the statements as many times as necessary.
ANS: T
3. True/False: In flowcharting, the decision structure and the repetition structure both use the diamond symbol to represent the condition that is tested.
ANS: T
4. True/False: The first line in the while loop is referred to as the condition clause.
ANS: F
5. True/False: In Python, an infinite loop usually occurs when the computer accesses the wrong memory address.
ANS: F
6. True/False: 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. True/False: 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 correct data.
ANS: T
8. True/False: Functions can be called from statements in the body of a loop, and loops can be called from the body of a function.
ANS: F
9. True/False: In a nested loop, the inner loop goes through all of its iterations for every single iteration of an outer loop.
ANS: T
10. True/False: To get the total number of iterations of a nested loop, multiply the number of iterations of all the loops.
ANS: T
FILL IN THE BLANK
1. A(n) _______________ structure causes a statement or 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 a condition is true.
ANS: condition
3. The while loop is known as a(n) _______________ loop because it tests conditions before performing an iteration.
ANS: pretest
4. A(n) _______________ loop usually occurs when the programmer forgets to write 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 a 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
Leave a reply