C++ || Chapter 5
To allow file access in a program, you must use ________ in the header file. a. #include file b. #include fstream c. #include fileaccess d. #include cfile
#include fstream
In the following statement, which operator is used first? while (x++ < 10) a. ++ b. cannot tell without rest of code c. neither; expression is invalid d. <
++
These are operators that add and subtract one from their operands a. conditional relational b. plus and minus c. binary and unary d. binary and ternary e. ++ and --
++ and --
What will the following code display? int x=0; while (x<5) { cout << x << " "; x++; } a. 0 1 2 3 4 5 b. 0 1 2 3 4 c. inifinite loop d. 0 1 2 3 4
0 1 2 3 4
What is the output of the following code segment? n = 1; while (n<=5) cout << n << ' '; n++; a. 2 3 4 5 b. 1 2 3 4 5 c. 1 1 ... and on forever d. 1 2 3 4
1 1 ... and on forever
How many times will the following loop display "Hello world!"? for (in i=0; i<20; i++) cout << "Hello world!" << endl; a. 20 b. 21 c. 19 d. infinite
20
How many times will the following loop display "Looping!"? for (int i=20; i>0; i--) cout << "Looping!" << endl; a. 21 b. 19 c. 20 d. infinite
20
How many times will the following loop diplay "Looping again!"? for (int i=0; i<=20; i++) cout << "Looping again!" << endl; a. 21 b. 20 c. 19 d. infinite number of times
21
What will the following code display? int x = 0; for (int count =0; count <3; count++) x += count; cout << x << endl;
3
What will the following code display? int number = 6; int x = 0; x = --number; cout << x << endl; a. 7 b. 0 c. 5 d. 6
5
What will the following code display? int number = 6; cout << number++ << endl; a. 0 b. 7 c. 5 d. 6
6
What will the following code display? int number = 6; int x = 0; x = number--; cout << x << endl; a. 5 b. 6 c. 0 d. 7
6
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
What will the following code display? int number = 6; number++; cout << number << endl; a. 6 b. 5 c. 0 d. 7
7
A while loop is somewhat limited because the counter can only be incremented by one each time through the loop. T/F
False
The condition that is tested by a while loop must be enclosed in parentheses and terminated with a semicolon. T/F
False
The increment and decrement operators can be used in mathematical expressions; however, they cannot be used in relational expressions. T/F
False
The scope of a variable declared in a for loop's initialization expression always extends beyond the body of the loop. T/F
False
You may nest while and do-while loops, but you may not nest for loops. T/F
False
You may not use both break and continue statements within the same set of nested loops. T/F
False
You may not use the break statement in a nested loop. T/F
False
A while loop's body can contain multiple statements, as long as they are enclosed in braces. T/F
True
An initialization expression may be omitted from the for loop if no initialization is required. T/F
True
An output file is a file that data is written to. T/F
True
If you want to stop a loop before it goes through all of its iterations, the break statement may be used. T/F
True
In C++ 11 you can pass a string object as an argument to a file stream object's open member function T/F
True
It is possible to define a file stream object and open a file in one statement. T/F
True
Multiple relational expressions cannot be placed into the test condition of a for loop. T/F
True
The update expression of a for loop can contain more than one statement, for example: for(i=5; i<=10; i++, total+=sales) T/F
True
string objects have a member function named c_str that returns the contents of the object formatted as null-terminated C-string. T/F
True
The do-while loop is considered a. an infinite loop b. a post-test loop c. none of these d. a pre-test loop e. a counter-controlled loop
a post-test loop
The two important parts of a while loop are the expression that is tested for a true or false value and a. one line of code that is repeated once, if the expression is true b. a statement or block that is repeated only if the expression is false c. a statement or block that is repeated once, if the expression is true d. a statement or block that is repeated as long as the expression is true
a statement or block that is repeated as long as the expression is 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 a. as many times as the user wishes b. at least twice c. never d. at least once
at least once
A statement that causes a loop to terminate early is a. terminate b. continue c. break d. re-iterate
break
A file _________ is a small holding section of memory that file-bound information is first written to a. number b. segment c. buffer d. name
buffer
Assuming dataFile is a file stream object, the following statement: dataFile.close(); a. is illegal in C++ b. closes a file d. is legal but risks losing data e. needs a filename argument to execute correctly
closes a file
A statement that may be used to stop a loop's current iteration and begin the next one is a. continue b. re-iterate c. terminate d. break
continue
A variable that is regularly incremented or decremented each time a loop iterates is a a. null terminator b. none of these c. constant d. counter e. control
counter
The __________ loop is ideal in situations where you want the loop to iterate at least once a. do-while b. for c. while d. switch
do-while
If you want a user to enter exactly 20 values, which loop would be best to use? a. do-while b. for c. switch d. while
for
The _______ loop is a good choice when you know how many times you want the loop to iterate in advance of entering the loop a. while b. do-while c. pre-test d. switch e. for
for
To write read data from a file, you define an object of the _________ data type. a. inputFile b. ofstream c. ifstream d. fstream
ifstream
This means to increase a value
increment
Something within a while loop must eventually cause the condition to become false or a __________ results a. infinite loop b. unexpected exit c. compiler error d. null value
infinite loop
In a for statement, this expression is executed only once a. null b. initialization c. the test d. validation
initialization
This is a control structure that causes a statement or group of statements to repeat. a. loop b. decision statement c. none of these d. cout object e. selection structure
loop
A loop that is inside another loop is called a a. infinite loop b. pre-test loop c. post-test loop d. nested loop
nested loop
To write data to a file, you define an object of the _______ data type a. ofstream b. outputFile c. ifstream d. fstream
ofstream
A file must be __________ before data can be written to or read from it a. closed b. opened c. named d. initialized
opened
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? a. outFile >> number; b. write(outFile, number); c. outFile << number; d. number >> outFile
outFile << number;
If you place a semicolon after the test expression in a while loop, it is assumed to be a a. null statement b. pre-test loop c. infinite loop d. post-test loop
post-test loop
The while loop is a ________ loop a. post-test b. limited c. pre-test d. infinite
pre-test
When the increment operator preceds its operant, as ++num, the expression is in ______ mode a. preliminary b. postfix c. prefix e. binary
prefix
This operator increments the value of its operand and then uses the value in context a. postfix increment b. postfix decrement c. prefix increment d. prefix decrement
prefix increment
A special value that marks the end of a list of values is a a. variable b. counter c. constant d. sentinel
sentinel
To write information to a file, use the a. stream insertion operator b. cout object c. pen object d. output object
stream insertion operator
A for statement contains three expressions: initialization, test, and a. update b. reversal c. null
update
You may define a ________ in the initialization expression of a for loop. a. none of these b. new data type c. function d. variable e. constant
variable
The ___________ loop is a good choice when you do not want the loop to iterate if the condition is FALSE in the beginning a. switch b. post-test c. while d. infinite e. do-while
while