COMSC Chapter 5 Practice
What will the following loop display? int x = 0; while (x < 5) { cout << x << endl; x++; } ● 0 1 2 3 4 5 ● 0 1 2 3 4 ● 0 1 2 3 4 ● The loop will display numbers starting at 0, for infinity.
0 1 2 3 4
What is the output of the following code segment? n = 1; for ( ; n <= 5; ) cout << n << ' '; n++; 2 3 4 5 6 1 1 1 ... and on forever 1 2 3 4 5 2 3 4 5 1 2 3 4
1 1 1 ... and on forever
What is the output of the following code segment? n = 1; while (n <= 5) cout << n << ' '; n++; 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 1 1... and on forever 2 3 4 5
1 1 1... and on forever
How many times will the following loop display "Hello"? for (int i = 1; i < 20; i++) cout << "Hello!" << endl; 20 19 21 An infinite number of times
19
How many times will the following loop display "Hello"? for (int i = 20; i > 0; i--) cout << "Hello!" << endl; 20 19 21 An infinite number of times
20
How many times will the following loop display "Hello"? for (int i = 0; i <= 20; i++) cout << "Hello!" << endl; 20 19 21 An infinite number of times
21
What will the following code display? int number = 6; cout << ++number << endl; 6 5 7 0
7
In a for statement, this expression is executed only once. test ●initialization ●null ●validation ●None of these
initialization
The while loop contains an expression that is tested for a true or false value, and a statement or block that is repeated as long as the expression: does not evaluate to true or false evaluates to true or false is true is false None of these
is true
The while loop is this type of loop. infinite post-test limited pre-test None of these
pre-test
What will the following code display? int x = 0; for (int count = 0; count < 3; count++) x += count; cout << x << endl; ●6 ●3 ●0 ●0 1 2
3
What will the following code display? int number = 6 int x = 0; x = --number; cout << x << endl; 6 5 7 0
5
What will the following code display? int number = 6; int x = 0; x = number--; cout << x << endl; 6 5 7 0
6
What will the following code display? int number = 6; cout << number++ << endl; 6 5 7 0
6
What will the following code display? int number = 6; ++number; cout << number << endl; 6 5 7 0
7
What will the following code display? int number = 6; number++; cout << number << endl; 6 5 7 0
7
Look at the following statement. while (x++ < 10) Which operator is evaluated first? < ++ Neither. The expression is invalid.
<
The while loop has two important parts: an expression that is tested for a true or false value, and: A) a statement or block that is repeated as long as the expression is true B) a statement or block that is repeated once, if the expression is true C) a statement or block that is repeated only if the expression is false D) one line of code that is repeated once, if the expression is true
A) a statement or block that is repeated as long as the expression is true
True/False: A while loop is somewhat limited, because the counter can only be incremented by one each time through the loop.
False
True/False: The increment and decrement operators can be used in mathematical expressions; however, they cannot be used in relational expressions.
False
True/False: The scope of a variable declared in a for loop's initialization expression always extends beyond the body of the loop.
False
True/False: You may nest while and do-while loops, but you may not nest for loops.
False
True/False: You may not use the break and continue statements within the same set of nested loops.
False
True/False: You may not use the break statement in a nested loop.
False
True/False: A while loop's body can contain multiple statements, as long as they are enclosed in braces.
True
True/False: An initialization expression may be omitted from the for loop if no initialization is required.
True
True/False: An output file is a file that data is written to.
True
True/False: If you want to stop a loop before it goes through all its iterations, the break statement may be used.
True
True/False: In C++ 11 you can pass a string object as argument to a file stream object's open member function.
True
True/False: It is possible to define a file stream object and open a file in one statement.
True
True/False: Multiple relational expressions cannot be placed into the test condition of a for loop.
True
True/False: The update expression of a for loop can contain more than one statement, e.g. counter++, total+= sales.
True
True/False: string objects have a member function named c_str that returns the contents of the object formatted as a null-terminated C-string.
True
A loop that is inside another loop is called: a pre-test loop a nested loop a post-test loop an infinite loop None of these
a nested loop
The statements in the body of a while loop may never be executed, whereas the statements in the body of a do-while loop will be executed: at least twice never at least once as many times as the user wishes None of these
at least once
This statement causes a loop to terminate early. ●terminate ●break ●null ●stop ●None of these
break
This statement may be used to stop a loop's current iteration and begin the next one. re-iterate break terminate continue None of these
continue
This is a variable that is regularly incremented or decremented each time a loop iterates. counter null terminator constant control statement None of these
counter
If you want a user to enter exactly 20 values, which loop would be the best to use? for do-while infinite while None of these
for
This loop is a good choice when you know how many times you want the loop to iterate in advance of entering the loop. ●while ●do-while ●for ●infinite ●None of these
for
This means to increase a value by one. decrement parse increment modulus None of these
increment
Something within a while loop must eventually cause the condition to become false, or a(n) ________ results. infinite loop compiler error null value unexpected exit None of these
infinite loop
This is a control structure that causes a statement or group of statements to repeat. constant cout object decision statement loop None of these
loop
If you place a semicolon after the test expression in a while loop, it is assumed to be a(n): post-test loop null statement pre-test loop infinite loop None of these
null statement
The do-while loop is considered a(n) ________ loop. pre-test limited infinite post-test None of these
post-test
When the increment operator precedes its operand, as in ++num1, the expression is in this mode. binary preliminary prefix postfix None of these
prefix
This operator increments the value of its operand, then uses the value in context. prefix decrement prefix increment postfix decrement postfix increment None of these
prefix increment
This is a special value that marks the end of a list of values. variable loop constant sentinel None of these
sentinel
A for statement contains three expressions: initialization, test, and reversal null update validation None of these
update
You may define a ________ in the initialization expression of a for loop. new data type function constant variable None of these
variable
This is a pre-test loop that is ideal in situations where you do not want the loop to iterate if the condition is false from the beginning. do-while while for infinite None of these
while