COP3014 Exam 2
In a for statement, this expression is executed only once:
initialization
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
How many times will the following display "Hello world!"? for (int i = 0; i < 20; i++) cout << "Hello world!" << endl;
20
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 display "Looping again!"? for (int i = 0; i <= 20; int++) cout << "Looping again!" << endl;
21
If you want a user to enter exactly 20 values, which loop would be the best to use?
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.
for
If a function doesnt have a prototype, default arguments can be specified in the ____
function header
written response
int count; for (count = 1; count <= 5; count++) cout << "Hello" << endl;
To write information to a file, use the
stream insertion operator
___ functions may have the same name as long as their parameter lists are different
two or more
A for statement contains three expressions, initialization, test, and
update
alt of above (just take out sum part)
#include <iostream> #include <iomanip> #include <fstream> using namespace std; int main() { int sum = 0; int x; ifstream inFile; inFile.open("test.txt"); if (!inFile) { cout << "Unable to open file"; exit(1); // terminate with error } while (inFile >> x) { sum = sum + x; } inFile.close(); cout << "Sum = " << sum << endl; return 0; }
written response 2
// reading a text file #include <iostream> #include <fstream> #include <string> using namespace std; int main () { string line; ifstream myfile ("example.txt"); if (myfile.is_open()) { while ( getline (myfile,line) ) { cout << line << '\n'; } myfile.close(); } else cout << "Unable to open file"; return 0; }
What will the following code display? int x = 0; while (x < 5) { cout << x << " "; x++; }
1 2 3 4
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; cout << x << endl;
6
What will the following code display? int number = 6; int x = 0; x = number--; cout << x << endl;
6
What will this code display? int number = 6; cout << ++number << endl;
6
What will the following code display? int number = 6; number++l cout << number << endl;
7
The ___ loop is ideal in situations where you want the loop to iterate AT LEAST ONCE.
DO-WHILE
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
A file ____ is a small holding section of memory that file-bound information is first written to
buffer
To write data to a file, you define an object for the ___ data type
ofstream
The WHILE loop is a ____ loop
pre-test