Practice Exam

This practice exam should give you some idea of what will be on the real exam. It is by no means comprehensive since, you know, I have to have something left to ask you on the real exam.


1. The code below contains five errors. What are they?


#include <iostream>

using namespace std

int main()
{
	int	i;
	char	c;

	cout << Please enter an integer << endl;
	cin >> i;

	cout << "Please enter a character" << endl;
	cin >> c;

	if( i = 0 )
	{
		cout << "That's a lame integer" << endl;
	}
	else if( c == F )
	{
		cout << "You don't want an F, do you?" << endl;
	}
	else
	{
		cout << "You have chosen .... wisely." << endl;

	return 0;
}

2. What value does i have after the following loop has run? How many times does this loop run?

i = 0;
while ( i < number )
{
	number = number * number;
	i++;
}

3. Given an integer, write a loop that calculates the sum of successive fractions. What this means is that if the integer is 5, the loop should calculate 1/1 + 1/2 + 1/3 + 1/4 + 1/5. If the integer is 8, it should calculate 1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8. And so on.


4. The following code contains five errors. What are they?
#include <iostream>

using namespace std;

float square(float f)

int main()
{
	float	num;

	cout << "Enter a number" << endl;
	cin >> num;

	cout << "The square of " << num << " is " << square;

	return 0;
}

int square(float f);
{
	f = num*num;
	return f;
}

5. What will the following program output? Why?

#include <iostream>

using namespace std;

float inverse(float f);

int main()
{
	float	num;

	cout << "Enter a number" << endl;
	cin >> num;

	inverse(num);

	cout << "The number inverted is " << num << endl;

	return 0;
}

float inverse(float f)
{
	f = 1/f;
	return f;
}