CO211 Final Exam Answers

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; //valid only for the scope of the if statement
		i = i+1;
	}
	cout << a << ' ' << i /* undefined! */ << endl;
	return 0;
}
ANSWER the object “i” in the cout statement at the end of the program is undefined; the previous declaration has run out of scope.
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 outfile("exam.txt");
	if(!outfile) {
		cerr << "Can't open \"exam.txt\" for writing." << endl;
		return 1;
	}
	
	for (int i=1; i <= 10; i++)
		outfile << i << endl;

	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++;
}
sum = 0;
for (i=0; i< 100; i++)
	sum += 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;
}
ANSWER
11
993
64
-110
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; i < size; i++)	//fill in here

		if (smallestSoFar > a[i])	//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();

	m2.promptAndInput();
	money sum(0,0);
	sum.add(m1);
	sum.add(m2);
	sum.print();

	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;
}
ANSWER
12
4 2 999 4
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;
}
ANSWER
150
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;
	sum = 0;
	while (cin >> i )	//fill in
		sum = sum + i;	//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.
void swap (char& c1, char& c2) {
	char temp = c1;
	c1 = c2;
	c2 = temp;
}