Preview (3 of 9 pages)

Preview Extract

Chapter 5: Looping
TRUE/FALSE
1. The loop control variable is initialized after entering the loop.
Answer: False
2. In some cases, a loop control variable does not have to be initialized.
Answer: False
3. An indefinite loop is a loop that never stops.
Answer: False
4. You can either increment or decrement the loop control variable.
Answer: True
5. When one loop appears inside another is is called an indented loop.
Answer: False
6. Forgetting to initialize and alter the loop control variable is a common mistake that
programmers sometimes make.
Answer: True
7. Every high-level computer programming language contains a while statement.
Answer: True
8. Both the while loop and the for loop are examples of pretest loops.
Answer: True
9. The safest action is to assign the value 1 to accumulators before using them.
Answer: False
10. It is the programmer’s responsibility to initialize all variables that must start with a
specific value.
Answer: True
MULTIPLE CHOICE
1. The first step in a while loop is typically to ____.
a. compare the loop control variable to a constant value
b. initialize the loop control variable
c. increment the loop control variable
d. execute the body of the loop
Answer: B

2. Once your logic enters the body of a structured loop, ____.
a. the entire loop must execute
b. the loop can be terminated with a break statement
c. the loop will execute indefinitely
d. a decision statement will be evaluated
Answer: A
3. The last step in a while loop is usually to ____.
a. compare the loop control variable to a constant value
b. initialize the loop control variable
c. increment the loop control variable
d. execute the body of the loop
Answer: C
4. A(n) ____ loop executes a predetermined number of times.
a. terminal
b. definite
c. indefinite
d. infinite
Answer: B
5. Many loop control variable values are altered by ____, or adding to them.
a. incrementing
b. decrementing
c. accumulating
d. deprecating
Answer: A
6. A(n) ____ is any numeric variable you use to count the number of times an event has
occurred.
a. accumulator
b. key
c. index
d. counter
Answer: D

7. A loop within another loop is known as a(n) ____ loop.
a. indefinite
b. infinite
c. nested
d. hidden
Answer: C
8. When one loop appears inside another, the loop that contains the other loop is called the
____ loop.
a. indefinite
b. definite
c. inner
d. outer
Answer: D
9. Usually, when you create nested loops, each loop has its own ____.
a. sentinel value
b. goto statement
c. entrance condition
d. loop control variable
Answer: D
10. A mistake programmers often make with loops is that they ____.
a. initialize the loop control variable prior to entering the loop body
b. increment the loop control variable inside of the loop body
c. include statements inside the loop that belong outside the loop
d. enclose the inner loop entirely within the outer loop in a nested loop
Answer: C
11. A mistake programmers often make with loops is that they ____.
a. neglect to initialize the loop control variable prior to entering the loop body
b. increment the loop control variable inside of the loop body
c. validate data to ensure values are the correct data type or that they fall within an acceptable
range
d. enclose the inner loop entirely within the outer loop in a nested loop

Answer: A
12. A comparison is correct only when the correct ____ and operator are used.
a. expression
b. operands
c. statements
d. index
Answer: B
13. Programmers use the term ____ to describe programs that are well designed and easy to
understand and maintain.
a. industrial
b. intractable
c. well behaved
d. elegant
Answer: D
14. You usually use the for loop with ____ loops.
a. indefinite
b. definite
c. inner
d. outer
Answer: B
15. The ____ loop provides three actions in one compact statement.
a. for
b. while
c. do until
d. repeat
Answer: A
16. The amount by which a for loop control variable changes is often called a ____ value.
a. group
b. key
c. step
d. sentinel

Answer: C
17. In a ____, the loop body might never execute because the question controlling the loop
might be false the first time it is asked.
a. summary report
b. pretest loop
c. posttest loop
d. loop control
Answer: B
18. In a ____, the loop body executes at least one time because the loop control variable is
not tested until after one iteration.
a. stub
b. pretest loop
c. posttest loop
d. loop control
Answer: C
19. The ____ loop is particularly useful when processing arrays.
a. for
b. while
c. infinite
d. nested
Answer: A
20. A(n) ____ is very similar to a counter that you use to count loop iterations, except that
you usually add a value other than one to this type of variable.
a. transaction
b. total
c. validator
d. accumulator
Answer: D
21. ____ is a technique with which you try to prepare for all possible errors before they
occur.
a. Incrementing
b. Stub

c. Nested loop
d. Defensive programming
Answer: D
22. Business reports that list only totals, with no individual item details, are called ____.
a. detail reports
b. summary reports
c. transaction reports
d. control-break reports
Answer: B
23. Loops are frequently used to ____; that is, to make sure it is meaningful and useful.
a. validate data
b. restructure data
c. simulate data
d. corroborate data
Answer: A
24. Programmers employ the acronym ____ to mean that if your input is incorrect, your
output is worthless.
a. IOW
b. GIGO
c. IOCW
d. GIGS
Answer: B
25. ____ a data item means you override incorrect data by setting the variable to a specific
value.
a. Flexing
b. Tracing
c. Forcing
d. Blanking
Answer: C
COMPLETION
1. Adding to a variable is called ____________________ the variable.

Answer: incrementing
2. When one loop appears inside another, the loop that is contained is called the
____________________ loop.
Answer: inner
nested
3. In a for statement, a loop control variable is initialized, evaluated, and
____________________.
Answer: altered
changed
incremented
decremented
4. ____________________ are frequently used to accumulate totals and to validate data.
Answer: Loops
loops
5. Making sure data falls within an acceptable range is referred to as ____________________
the data.
Answer: validating
MATCHING
Match each term with a statement below.
1. One set of instructions that operates on multiple, separate sets of data a. indefinite
2. Initialized before entering a while loop b. loop control variable
3. To add to a variable c. nested loops
4. To decrease a variable’s value d. counter
5. Any numeric variable you use to count the number of times an event has occurred e.
increment
6. A loop that may execute a different number of times each time the program executes f.
decrement
7. A value such as Y or N that stops a loop g. accumulator
8. Loops within loops h. loop
9. An empty module that acts as a placeholder i. sentinel value
10. A variable that you use to gather values j. stub
1. Answer: H

2. Answer: B
3. Answer: E
4. Answer: F
5. Answer: D
6. Answer: A
7. Answer: I
8. Answer: C
9. Answer: J
10. Answer: G
SHORT ANSWER
1. What is an advantage of using fewer instructions?
Answer: Using fewer instructions not only results in less time required for design and coding,
but in less compile time and reduced errors.
2. What are the three steps that should occur in every loop?
Answer: 1. The loop control variable is initialized before entering the loop.
2. The loop control variable is tested, and, if the result is true, the loop body is entered.
3. The loop control variable is altered within the body of the loop so that the while
expression eventually evaluates as false.
3. What is the difference between a definite loop and an indefinite loop?
Answer: A definite loop executes a definite, predetermined number of times. An indefinite
loop is indefinite because each time the program executes, the loop might be performed a
different number of times.
4. Explain when it would be appropriate to write an indefinite loop.
Answer: Often, the value of a loop control variable is not altered by arithmetic, but instead is
altered by user input. For example, perhaps you want to continue performing some task while
the user indicates a desire to continue. In that case, you do not know when you write the
program whether the loop will be executed two times, 200 times, or not at all. This type of
loop is an indefinite loop.
5. What are common mistakes made by programmers in coding loops?
Answer: • Neglecting to initialize the loop control variable
• Neglecting to alter the loop control variable
• Using the wrong comparison with the loop control variable
• Including statements inside the loop that belong outside the loop

6. What are the tasks performed by the for count = 0 to 3 statement?
Answer: This for statement accomplishes several tasks at once in a compact form:
• The for statement initializes count to 0.
• The for statement checks count against the limit value 3 and makes sure that count is less
than or equal to that value.
• If the evaluation is true, the for statement body executes.
• After the for statement body executes, the value of count increases by 1, and the comparison
to the limit value is made again.
7. What is a step value?
Answer: The amount by which a for loop control variable changes is often called a step
value. The step value can be positive or negative; that is, it can increment or decrement.
8. When are you required to use a for statement, and what are the advantages of for
statements?
Answer: You never are required to use a for statement for any loop; a while statement can
always be used instead. However, when a loop’s execution is based on a loop control variable
progressing from a known starting value to a known ending value in equal steps, the for loop
provides you with a convenient shorthand. It is easy for others to read; and because the loop
control variable’s initialization, testing, and alteration are all performed in one location, you
are less likely to leave out one of these crucial elements.
9. What is an accumulator?
Answer: An accumulator is a variable that you use to gather or accumulate values. An
accumulator is very similar to a counter that you use to count loop iterations. However,
usually you add just one to a counter, whereas you add some other value to an accumulator.
10. How might user data be validated?
Answer: When you ask a user to enter data into a computer program, you have no assurance
that the data the user enters will be accurate. Loops are frequently used to validate data; that
is, to make sure it falls within an acceptable range. For example, suppose part of a program
you are writing asks a user to enter a number that represents his or her birth month. If the user
types a number less than 1 or greater than 12, you must take some sort of action. For
example:
• You could display an error message and stop the program.
• You could choose to assign a default value for the month (for example, 1) before
proceeding.
• You could reprompt the user for valid input.

Test Bank for Programming Logic and Design
Joyce Farrell
9781111969752, 9788131525906, 9781111825959

Document Details

Related Documents

Close

Send listing report

highlight_off

You already reported this listing

The report is private and won't be shared with the owner

rotate_right
Close
rotate_right
Close

Send Message

image
Close

My favorites

image
Close

Application Form

image
Notifications visibility rotate_right Clear all Close close
image
image
arrow_left
arrow_right