Chapter 5 c++
What is the output of the following code segment? n = 1; while (n<=5) cout << n << ' ' ; n++;
1 1....... and on forever
How many times will the following loop display "Hello world"? for ( int i = 0; i <= 20; i++) cout << "Hello world!" << endl;
20
What will the following code display? int number = 6 ; cout << number++ << endl;
6
What will the following code display? int number = 6; int x = 0; x = number--; cout << x << endl;
6
In the following statement, which operator is used first? while (x++ < 10 )
<
What will the following code display? int x = 0 ; while ( x < 5) { cout << x << " " ; x++; }
0 1 2 3 4
These are operators that add and subtract one from their operands.
++ and --
How many times will the following loop display "Looping"? for ( int i = 20 ; i > 0; i --) cout << "Looping!" << endl;
20
How many times will the following loop display "Looping again!"? for ( int i = 0; i <= 20; i++) cout << "Looping again!" << endl;
21
What will the following code display? int number = 6; ++number; cout << number << endl;
7
To allow file access in a program, you must include the ______ header file.
<fstream>
A while loop is somewhat limited because the counter can only be incremented by one each time through the loop.
False
The condition that is tested by a while loop must be enclosed in parentheses and terminated with a semicolon.
False
The scope of a variable declared in a for loop's initialization expression always extends beyond the body of the loop.
False
You may nest while and do-while loops, but you may not nest for loops.
False
You may not use both break and continue statements within the same sets of nested loops.
False
You may not use the break statement in a nested loop.
False
A while loop's body can contain multiple statements, as long as they are endorsed in braces.
True
An initialization expression may be omitted from the for loop if no initialization is required.
True
An output file is a file that data is written to.
True
It is possible to define a file stream object and open a file in one statement.
True
Multiple relational expressions cannot be placed into the test condition of a for loop.
True
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 statement that causes a loop to terminate early is
break
Assuming dataFile is a file stream object, the following statement dataFile.close( );
closes a file
In a for statement, this expression is executed only once
initialization
If you place a semicolon after the test expression in a while loop, it is assumed to be a(n):
null statement
To write data to a file, you define an object of the __________ data type.
ofstream
A file must be _______ before data can be written to or read from it.
opened
This while loop is a(n) ________ loop.
pre-test
When the increment operator precedes its operand, as in ++num1, the expression is in this mode.
prefix
This operator increments the value of its operand, then uses the value in context.
prefix increment
A special value that marks the end of a list of values is a
sentinel
The ______ loop is a good choice when you do not want the loop to iterate if the condition is false in the beginning.
while
The do- while loop is considered
a post-test loop
The _____ loop is ideal in situations where you want the loop to iterate at least once.
do-while
You may define a ________ in the initialization expression of a for loop.
variable
What will the following code display? int number = 6; int x = 0; x = --number; cout << x << endl;
5
What will the following code display? int x = 0 ; for ( int count = 0; count < 3; count++) x += count; count << x << endl;
3
What will the following code display? Int number = 6; number ++; cout << number << endl;
7
What will the following code display? int number = 6; cout << ++number << endl;
7
The two important parts of a while loop are the expression that is tested for a true or false value and
a statement or block that is repeated as long as the expression is true
In C++ 11 and later, you can press a string object as an argument to a file stream object's open member function.
True
If you want a user to enter exactly 20 values, which loop would be the best to use?
for
Assuming outFile is a file stream object and number is a variable, which statement writes the contents of number to the file associated with outFile?
outFile << number;
A file ________ is a small holding section of memory that file- bound information is first written to.
buffer
A statement that may be used to stop a loop's current iteration and begin the next one is
continue
A variable that is regularly incremented or decremented each time a loop iterates is a
counter
To write information to a file, use the
stream insertion operator
A for statement contains three expressions: initializations, test, and
update
The increment and decrement operators can be used in mathematical expressions, however, they cannot be used in relational expressions.
false
The ______ loop is a good choice when you know how many times you want the loop to iterate in advance of entering the loop.
for
To write read data from a file, you define an object of the ________ data type.
ifstream
This means to increase a value.
increment
Something within a while loop must eventually cause the condition to become false, or a(n) ________ results.
infinite loop
This is a control structure that causes a statement or group of statements to repeat.
loop
A loop that is inside another loop is called a(n)
nested loop
If you want to stop a loop before it goes through all of its iterations, the break statement may be used.
True
The update expression of a for loop can contain more than one statement, for example: for( i = 5; i <= 10; i++, total += sales)
True
The statements in the body of a while loop may never be executed while the statements in the body of a do-while loop will be executed
at least once