Deck 5: Looping

Full screen (f)
exit full mode
Question
The while loop is a(n) ________ loop, whereas the do-while loop is a(n) ________ loop.

A) finite, infinite
B) infinite, finite
C) simple, complex
D) pretest, post test
E) post test, pretest
Use Space or
up arrow
down arrow
to flip the card.
Question
What will the following code print?
Num = 8;
Cout << --num << " ";
Cout << num++ << " ";
Cout << num;

A) 7 7 8
B) 7 8 8
C) 8 7 7
D) 8 7 8
E) None of these
Question
An initialization expression may be omitted from the for loop if no initialization is required.
Question
The ++ operator

A) is a unary operator.
B) adds one to the value of its operand.
C) can operate in prefix or postfix mode.
D) All of the above.
E) Both B and C, but not A.
Question
In a for statement, the ________ expression is executed only once.

A) initialization
B) test
C) repeat
D) validate
E) update
Question
A for statement contains three expressions: initialization, test, and

A) repeat.
B) validate.
C) update.
D) quit.
E) increment.
Question
A while loop is somewhat limited, because the counter can only count up, not down.
Question
To use files in a C++ program you must include the ________ header file.

A) file
B) iostream
C) fstream
D) Both A and B.
E) Both B and C.
Question
The statements in the body of a do-while loop are executed

A) exactly once.
B) at least once.
C) only if the test condition is initially True.
D) until the test condition becomes True.
E) forever until the user hits the break key.
Question
A variable that keeps a running total of data values is called a(n)

A) total.
B) sum.
C) accumulator.
D) counter.
E) loop control variable.
Question
The block of code in the body of a while statement can contain an unlimited number of statements, provided they are enclosed in a set of braces.
Question
To use an output file in a C++ program you must

A) make sure the file already exists.
B) create a file stream object that will "point to" (i.e. reference) the file.
C) open the file.
D) do all of the above.
E) do B and C, but not A.
Question
For data validation, it is best to use a(n)

A) if statement.
B) while loop.
C) for loop.
D) nested loop.
E) switch statement.
Question
The while loop has two important parts: a condition that is tested and a statement or block of statements that is

A) repeated as long as the condition is True.
B) repeated until the condition becomes True.
C) done once if the condition is True.
D) always done at least once, then repeated if the condition is True.
E) always skipped.
Question
When a loop is nested inside another loop, the outer loop goes through all its iterations for each iteration of the inner loop.
Question
________ are C++ operators that change their operands by one.

A) + and -
B) ++ and --
C) binary and unary
D) arithmetic and relational
E) conditional and relational
Question
A sentinel is a special value that

A) is used for data validation.
B) must be Boolean.
C) marks the end of a list of values.
D) must be a negative number.
E) is all of the above.
Question
You may define a(n) ________ in the initialization expression of a for loop.

A) new data type
B) new keyword
C) constant
D) variable
E) operator
Question
A while loop may have a semicolon after the test expression and before the body of the loop, but it is not required.
Question
To decrement a number means to increase its value.
Question
The ideal type of loop to use if you want a user to enter exactly 20 values is a(n) ________ loop.

A) do-while
B) for
C) sentinel controlled
D) infinite
E) nested
Question
You can nest a for loop inside another for loop, but cannot nest a while loop inside another while loop or a do-while loop inside another do-while loop.
Question
When a loop is nested inside another loop, the inner loop goes through all its iterations for each iteration of the outer loop.
Question
To ________ a value means to increase it.

A) decrement
B) increment
C) add up
D) accumulate
E) total
Question
A while loop is somewhat limited because the counter can only be incremented or decremented by one each time through the loop.
Question
If nothing within a while loop ever causes the condition to become false, a(n) ________ may occur.

A) null value
B) infinite loop
C) unexpected exit
D) compiler error
E) system crash
Question
To use files in a C++ program you must include the ________ header file.

A) fstream
B) iostream
C) file
D) Both A and B are needed.
E) Both B and C are needed.
Question
What will the following code print?
Num = 5;
Cout << num++ << " ";
Cout << num-- << " ";
Cout << --num;

A) 5 4 3
B) 5 5 4
C) 5 6 4
D) 5 6 5
E) 6 5 4
Question
The ideal type of loop to use for repeating a menu is a(n) ________ loop.

A) do-while
B) for
C) sentinel controlled
D) infinite
E) nested
Question
A(n) ________ is a variable that is regularly incremented or decremented each time a loop iterates.

A) counter
B) accumulator
C) sentinel
D) total
E) loop control variable
Question
The while loop has two important parts: a condition that is tested and a statement or block of statements that is executed

A) as long as the condition is True.
B) until the condition becomes True.
C) at least once.
D) exactly once.
E) Both A and C are True, but B and D are not.
Question
The statements in the body of a do-while loop are executed

A) only if the test condition is initially True.
B) at least once.
C) until the test condition becomes True.
D) until an exit statement is encountered.
E) forever until the user presses the break key.
Question
The ________ statement causes a loop to terminate early.

A) stop
B) break
C) quit
D) terminate
E) continue
Question
A(n) ________ is a variable that controls the number of times a loop iterates.

A) counter
B) accumulator
C) sentinel
D) total
E) loop control variable
Question
Before beginning to add value to it, an accumulator should be initialized to 1.
Question
A(n) ________ is a special value that marks the end of a list of values.

A) counter
B) accumulator
C) sentinel
D) total
E) loop control variable
Question
If a while loop has no braces around the body of the loop

A) there is no loop body.
B) the loop body ends when the endwhile statement is encountered.
C) the loop body contains just one statement.
D) the program will not compile.
E) the program will compile, but not run.
Question
The -- operator

A) is a unary operator.
B) subtracts one from the value of its operand.
C) must have an lvalue, such as a variable, as its operand.
D) can be used in either prefix or postfix mode.
E) All of the above are True.
Question
The do-while loop is a(n) ________ loop, whereas the while loop is a(n) ________ loop.

A) finite, infinite
B) infinite, finite
C) simple, complex
D) pretest, post test
E) post test, pretest
Question
In order for a C++ program to read data from a file,

A) the file must already exist.
B) the program must define a file stream object that can "point to" (i.e., reference) the file.
C) the program must open the file.
D) All of the above are required.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/40
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 5: Looping
1
The while loop is a(n) ________ loop, whereas the do-while loop is a(n) ________ loop.

A) finite, infinite
B) infinite, finite
C) simple, complex
D) pretest, post test
E) post test, pretest
D
2
What will the following code print?
Num = 8;
Cout << --num << " ";
Cout << num++ << " ";
Cout << num;

A) 7 7 8
B) 7 8 8
C) 8 7 7
D) 8 7 8
E) None of these
A
3
An initialization expression may be omitted from the for loop if no initialization is required.
True
4
The ++ operator

A) is a unary operator.
B) adds one to the value of its operand.
C) can operate in prefix or postfix mode.
D) All of the above.
E) Both B and C, but not A.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
5
In a for statement, the ________ expression is executed only once.

A) initialization
B) test
C) repeat
D) validate
E) update
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
6
A for statement contains three expressions: initialization, test, and

A) repeat.
B) validate.
C) update.
D) quit.
E) increment.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
7
A while loop is somewhat limited, because the counter can only count up, not down.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
8
To use files in a C++ program you must include the ________ header file.

A) file
B) iostream
C) fstream
D) Both A and B.
E) Both B and C.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
9
The statements in the body of a do-while loop are executed

A) exactly once.
B) at least once.
C) only if the test condition is initially True.
D) until the test condition becomes True.
E) forever until the user hits the break key.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
10
A variable that keeps a running total of data values is called a(n)

A) total.
B) sum.
C) accumulator.
D) counter.
E) loop control variable.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
11
The block of code in the body of a while statement can contain an unlimited number of statements, provided they are enclosed in a set of braces.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
12
To use an output file in a C++ program you must

A) make sure the file already exists.
B) create a file stream object that will "point to" (i.e. reference) the file.
C) open the file.
D) do all of the above.
E) do B and C, but not A.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
13
For data validation, it is best to use a(n)

A) if statement.
B) while loop.
C) for loop.
D) nested loop.
E) switch statement.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
14
The while loop has two important parts: a condition that is tested and a statement or block of statements that is

A) repeated as long as the condition is True.
B) repeated until the condition becomes True.
C) done once if the condition is True.
D) always done at least once, then repeated if the condition is True.
E) always skipped.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
15
When a loop is nested inside another loop, the outer loop goes through all its iterations for each iteration of the inner loop.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
16
________ are C++ operators that change their operands by one.

A) + and -
B) ++ and --
C) binary and unary
D) arithmetic and relational
E) conditional and relational
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
17
A sentinel is a special value that

A) is used for data validation.
B) must be Boolean.
C) marks the end of a list of values.
D) must be a negative number.
E) is all of the above.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
18
You may define a(n) ________ in the initialization expression of a for loop.

A) new data type
B) new keyword
C) constant
D) variable
E) operator
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
19
A while loop may have a semicolon after the test expression and before the body of the loop, but it is not required.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
20
To decrement a number means to increase its value.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
21
The ideal type of loop to use if you want a user to enter exactly 20 values is a(n) ________ loop.

A) do-while
B) for
C) sentinel controlled
D) infinite
E) nested
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
22
You can nest a for loop inside another for loop, but cannot nest a while loop inside another while loop or a do-while loop inside another do-while loop.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
23
When a loop is nested inside another loop, the inner loop goes through all its iterations for each iteration of the outer loop.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
24
To ________ a value means to increase it.

A) decrement
B) increment
C) add up
D) accumulate
E) total
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
25
A while loop is somewhat limited because the counter can only be incremented or decremented by one each time through the loop.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
26
If nothing within a while loop ever causes the condition to become false, a(n) ________ may occur.

A) null value
B) infinite loop
C) unexpected exit
D) compiler error
E) system crash
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
27
To use files in a C++ program you must include the ________ header file.

A) fstream
B) iostream
C) file
D) Both A and B are needed.
E) Both B and C are needed.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
28
What will the following code print?
Num = 5;
Cout << num++ << " ";
Cout << num-- << " ";
Cout << --num;

A) 5 4 3
B) 5 5 4
C) 5 6 4
D) 5 6 5
E) 6 5 4
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
29
The ideal type of loop to use for repeating a menu is a(n) ________ loop.

A) do-while
B) for
C) sentinel controlled
D) infinite
E) nested
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
30
A(n) ________ is a variable that is regularly incremented or decremented each time a loop iterates.

A) counter
B) accumulator
C) sentinel
D) total
E) loop control variable
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
31
The while loop has two important parts: a condition that is tested and a statement or block of statements that is executed

A) as long as the condition is True.
B) until the condition becomes True.
C) at least once.
D) exactly once.
E) Both A and C are True, but B and D are not.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
32
The statements in the body of a do-while loop are executed

A) only if the test condition is initially True.
B) at least once.
C) until the test condition becomes True.
D) until an exit statement is encountered.
E) forever until the user presses the break key.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
33
The ________ statement causes a loop to terminate early.

A) stop
B) break
C) quit
D) terminate
E) continue
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
34
A(n) ________ is a variable that controls the number of times a loop iterates.

A) counter
B) accumulator
C) sentinel
D) total
E) loop control variable
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
35
Before beginning to add value to it, an accumulator should be initialized to 1.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
36
A(n) ________ is a special value that marks the end of a list of values.

A) counter
B) accumulator
C) sentinel
D) total
E) loop control variable
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
37
If a while loop has no braces around the body of the loop

A) there is no loop body.
B) the loop body ends when the endwhile statement is encountered.
C) the loop body contains just one statement.
D) the program will not compile.
E) the program will compile, but not run.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
38
The -- operator

A) is a unary operator.
B) subtracts one from the value of its operand.
C) must have an lvalue, such as a variable, as its operand.
D) can be used in either prefix or postfix mode.
E) All of the above are True.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
39
The do-while loop is a(n) ________ loop, whereas the while loop is a(n) ________ loop.

A) finite, infinite
B) infinite, finite
C) simple, complex
D) pretest, post test
E) post test, pretest
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
40
In order for a C++ program to read data from a file,

A) the file must already exist.
B) the program must define a file stream object that can "point to" (i.e., reference) the file.
C) the program must open the file.
D) All of the above are required.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 40 flashcards in this deck.