Chapter 5
What will the following code display? int number = 4; int result = 2 * number++; cout << result << endl;
8
After you have attempted to open a file, you can test the following as if it were a Boolean expression, to determine whether the file was successfully opened.
file stream object
This is an object that is created in memory by a C++ program, and associated with a specific file. It provides a way for the program to work with that file.
file stream object
A loop that is inside another loop is called a(an):
nested loop
There are two general ways to access data stored in a file: sequential access and direct access.
true
What will the following code display? int number = 0; cout << number++ << endl;
0
What will the following code display? int number = 0; number++; --number; cout << number << endl;
0
When the following code executes what will be displayed? int num1, num2, result; for (num1 = 1, num2 = 1; num1 <= 3; num1++, num2++) { result = num1 - num2; } cout << result << endl;
0
When the following code executes, how many times will I love to code! be displayed? int number = 10; while (number < 10) { cout << "I love to code!\n"; number++; }
0
What will the following code display? int number = 0; number++; cout << number << endl;
1
What will the following code display? int x = 0; if (++x == 1) cout << 1 << endl; else cout << 0 << endl; return 0;
1
What will the following code display? int number = 0; number++; ++number; cout << number << endl;
2
This loop is specifically designed to initialize, test, and update a counter variable.
for
Which of these header files defines the data types ofstream , ifstream , and fstream?
fstream
When the following code executes, how many times will "Programming is awesome!" be displayed? int number = 3; while (number > 0) cout << "Programming is awesome!" << endl; number--; return 0;
infinitely
This is a file from which data is read:
input file
Each repetition of a loop is known as this:
iteration
When you work with this type of file, you access data from the beginning of the file to the end of the file.
sequential access
A file's read position marks this location in the file.
the next byte that will be read
A counter is a variable that is regularly incremented or decremented each time a loop iterates.
true
A sentinel is a special value that cannot be mistaken as a member of the list and signals that there are no more values to be entered.
true
All modifications of the for loop's counter variable should take place in the update expression, which is automatically executed at the end of each iteration.
true
Although a program's files are automatically closed when the program shuts down, it is a good programming practice to write statements that close them.
true
An important characteristic of the while loop is that the loop will never iterate if the test expression is false to start with.
true
Files on a disk are identified by a filename.
true
In C++ 11, you can pass a string object as argument to a file stream object's open member function.
true
In general, there are two categories of loops: conditional loops and count-controlled loops.
true
In general, there are two types of files: text and binary.
true
In most cases, loops must contain within themselves a way to terminate.
true
It is possible for the user to determine the values that will be used in the initialization and test expressions of a for loop.
true
It is possible to execute more than one statement in the initialization expression and the update expression of a for loop.
true
Postfix mode causes the increment to happen after the value of the variable is used in the expression.
true
Programmers usually refer to the process of saving data in a file as writing data to the file.
true
The do-while loop is a posttest loop. This means it does not test its expression until it has completed an iteration.
true
The do-while loop must be terminated with a semicolon.
true
The increment and decrement operators usually have variables for their operands, but generally speaking, anything that can go on the left side of an = operator is legal.
true
The programming style you should use with the while loop is similar to that of the if statement.
true
The variable that is used to accumulate the total of the numbers is called an accumulator.
true
The while loop has two important parts: (1) an expression that is tested for a true or false value, and (2) a statement or block that is repeated as long as the expression is true.
true
To get the total number of iterations of a nested loop, multiply the number of iterations of all the loops.
true
When a variable is defined in the initialization expression of a for loop, the scope of the variable is limited to the loop.
true
You can use the >> operator to read numeric data from a text file, into a numeric variable, and the >> operator will automatically convert the data to a numeric data type.
true
This type of loop allows the user to decide the number of iterations.
user-controlled loop
The while loop is a good choice for repeating a menu.
false
What will the following code display? int number = 4; int result = 2 * ++number; cout << result << endl;
10
This operator can be used with ofstream objects to write data to a file.
<<
This operator not only reads user input from the cin object, but also data from a file.
>>
Because the for loop tests its test expression after it performs an iteration, it is a posttest loop.
false
The while loop is known as this, which means it tests its expression before each iteration.
a pretest loop
In what situation should you use the for loop instead of the while or do-while loop? The loop requires an initialization. The loop uses a false test expression to stop. The loop performs an increment at the end of each iteration. All of these situations.
all of these situations
If a loop does not have a way of stopping, it is called:
an infinite loop
This type of file contains data that has not been converted to text.
binary file
The following statement causes a loop to terminate early.
break
Only increment statements may be used in the update expression of a for loop.
false
The following statement causes a loop to stop its current iteration and begin the next one.
continue
A loop that repeats a specific number of times is known as a:
count-controlled loop
This ifstream object member function returns true when an attempted file operation is unsuccessful.
fail
This is a control structure that causes a statement or group of statements to repeat.
loop
In the following code, which variable is the counter? const int MAX_VALUE = 10; int number = 0; int result = 0; while (number < MAX_VALUE) { result = number * 2; cout << result << endl; number++; }
number
What are the three steps, in order, that a program takes when using a file?
open, process, close
This is a sum of numbers that accumulates with each iteration of a loop.
running total