CSE 143 Quiz 1: July 2nd, 1998
Time limit: 20 minutes
Closed book, closed notes

Name:

Section:


Question 1:
Please circle true or false. (correct answer indicated in red)
1. True False   A major part of computer science is the design and construction of abstractions.
2. True False   A C++-style // comment terminates automatically at the end of a line.
3. True False   The two components of an abstraction are the interface and the specification
4. True False   The new stream I/O syntax in C++ reflects the fact that input and output are conceptually very different from C.
5. True False   Graceful error handling and recovery can help to debug and maintain programs.
6. True False   In C++, a variable declaration can appear anywhere a statement can.
7. True False   If a precondition is false at the entry to a function, it means that something is wrong with the runtime state of the program.
8. True False   When developing a program, you should avoid the assert macro, since it slows your program down.
9. True False   An ADT is a language-level construct that allows you to implement new classes.
10. True False   If a module's specification changes, it suffices to recompile all the clients of that module.

Question 2:
What is the longest river in Canada?

The McKenzie River. Please see this page for more geographical facts about Canada. This question will not be graded!

Question 3:
Give one reason why the use of abstraction can help make the task of writing software easier.

Several answers are possible here. But I was looking for something more than a definition of abstraction. Here's a sample:

"Abstraction can give you simple models for complex parts of your program, which helps you manage the complexity of the whole system."

Question 4:
The following file was written in C. It could benefit from some of the new features in C++. Identify three changes that could be made to use the new C++ features more effectively. You don't need to rewrite the function; just circle the code that could be changed (or eliminated) and give a few words saying what kind of change is needed.

	#include <math.h>

	#define FALSE 0
	#define TRUE 1  Could use built-in bool type here.
	#define PI 3.1415926 Use const double instead of #define

	int strangeIntegerTest( double d )
	{
		double s = sin( d * PI );

		if( s == 0.0 ) {
			printf( "%lf is an integer.\n", d ); Use cout instead of printf
			return TRUE; (use bool)
		} else {
			return FALSE; (use bool)
		}
	}

Question 5:
The following C++ function has a flaw - if the month argument is not in the range 0-11 (inclusive), the function will behave incorrectly. Choose one of the error handling mechanisms discussed in lecture (other than assuming the input is OK) and rewrite the function using that mechanism, so that this flaw is dealt with in some way.

	int numberOfDaysInMonth( int month )
	{
		static int days[ 12 ] = { 
			31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		return days[ month ];
	}
A possible solution, using the assert macro:
	#include <assert.h>

	int numberOfDaysInMonth( int month )
	{
		assert( month >= 0 && month <= 11 );

		static int days[ 12 ] = {
			31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		reutrn days[ month ];
	}