December 13, 1999
Each question is worth 10 points for a total of 100 points.
1. | What is wrong with the following program? (It will not compile.) #include <iostream> using namespace std; int main() { int a; cout << "Enter an integer: "; cin >> a; if (a>10) { int i=2; i = i+1; } cout << a << ' ' << i << endl; return 0; } |
2. | Write a program that will write the numbers 1,2,3,4,5,6,7,8,9,10 to a file, one per
line. The name of the file should be exam.txt #include <fstream> using namespace std; int main() { ofstream return 0; } |
3. | Write a for statement that is equivalent to the following while
statement: sum=0; i=0; while (i<100) { sum = sum+i; i++; }
|
4. | What is the output of the following program? #include <iostream> int AFunction(); int AFunction(int a); int AFunction(int a, int b); int main() { cout << AFunction(1,2) << endl; cout << AFunction(99) << endl; cout << AFunction() << endl; cout << AFunction(-10,-20) << endl; return 0; } int AFunction() { return 64; } int AFunction(int a) { return 10*a+3; } int AFunction(int a, int b) { return 3*a+4*b; } |
5. | This function is supposed to find the smallest element in an array. Fill in the blank
areas. int Minimum(int a[], size) { //returns the smallest element in the array a int smallestSoFar = a[0]; for (int i=1; ) //fill in here if ( ) //fill in here smallestSoFar = a[i]; return smallestSoFar; } |
6. | Here is a class declaration: class money { public: money(); money(int d, int c); void add(money m); //adds m to the owner void print(); //prints the owner void promptAndInput(); //prompts the users and reads in the values private: int dollars; int cents; } Use this class declaration to complete the following program that reads in two amounts of money and prints their sum. int main() { money m1, m2; m1.promptAndInput(); return 0; } |
7. | Exactly what does the following program print. #include <iostream> using namespace std; int function1(int a); void function2(int & b); void function3(int c); int main() { int i=2, j=3, k=4,m; m = function1(i); function2(j); function3(k); cout << m << ' ' << i << ' ' << j << ' ' << k << endl; return 0; } int function1(int a) { return 2*a; } void function2(int & b) { b = 999; return; } void function3(int c) { c = 3*c; cout << c << endl; return; } |
8. | What does this program print? #include <iostream> using namespace std; int main() { int a[100]; for (int i=0; i<100; i++) a[i] = 200 - i; cout << a[50] << endl; return 0; } |
9. | Fill in the blanks in the program below so it will read a list of integers from the
keyboard, with the end of the list indicated by [Ctrl-D] , and print out the
totals of the numbers in the list. #include <iostream> using namespace std; int main() { int i, sum; cout << "enter the list of integers, end with ^D" << endl; while ( ) //fill in sum = //fill in cout << "the total is: " << sum << endl; return 0; } |
10. | Write the complete declaration of a function swap that has two
character parameters and interchanges the values of its arguments, but does not return a
value. |