SDSU CS 596 OODP
Old Exams

[To Lecture Notes Index]
San Diego State University -- This page last updated Oct. 23, 1995
----------

Contents of Old Exams

  1. Midterm Fall 1994
  2. Final Fall 1994
  3. Final Fall 1993

Midterm Fall 1994

1. What is the difference between private and protected class members.

2. What is "this". Give an example of when you would use "this".

3. What are the initialization and the assignment phases of a constructor? What is each used for?

4. Having a pointer as a class data member can lead to some undesirable side effects. What should you add to a class to avoid these side effects?

Each of the following programs illustrates one or more concepts or issues in C++. What is/are those issues? What is the result of compiling and running each program? (Some programs may not compile.) Explain your answer. Assume iostream.h is included in each program. Any minor syntax errors are typos.

5a)
void you( long,  long ) {cout << "long"};
void you( double, double ) {cout << "double"};

main()
{
	int got, me;
	dont( got, me );				
}

5b)
main()
{
	const  char*  what  =  "Is This";	
	pc = "Interesting"					
	cout << *what;
	what[3] = 'a';					
	cout << *what
}

6a)
int& Now()  {
	int Where = 1;
	return Where ;
}

main() {
	int Where;
	Where= Now();
	cout << Where;		//
}

6b)
class Where {
	public:
		int here;
	
		int me() const;
		int you() const;
};

int Where ::me() const {
	this->here = here + 5;				
	return this->here;
}

int Where ::you() const {
	return here + 5;
}

int OhNo(const Where* Now)
{
	return Where->you();
}


main()
{
	Where* IsIt = new Where;
	cout << OhNo(IsIt);
};

7a)
class Top
{	public:
		int a;
		Top(int x) { a = x;}

};

class Bottom : public Top
{	public:
		int b;
		Bottom() { b = 0; a = 0}	};


void main()
{	
	Bottom barrel;
	cout << barrel.a << barrel.b;			
}

7b)
class Top
{
	public:
		Hat(char *string) {	cout << "Top";}
		Hat(float a) {	cout << "Top Too";}
};

class Bottom :  public Top
{	
	public:
		Hat(const int a) {	cout << "Bottom";}
		Hat(float a) {	cout << "Bottom Too";}
};

main()
{
	Bottom* Rung;
	Rung->Hat(5.5);
	Rung->Hat("cat");

}

8.
class Trouble
{	public:
	Trouble() { cout << "Start Trouble";}
	~Trouble() { cout << "End Trouble";}
};

class Big : public Trouble
{	public:
	Big() { cout << "Start Big";}
	~Big() { cout << "End Big";}
};

class Dont
{	public:
	Big deal;
	Dont() { cout << "Start Dont";}
};

main()
{
	Dont DoThisToMe;
}

Final Fall 1994

1. Give the output of the following program.
#include <iostream.h>

class Top {
public:
	virtual void MyMemory() { cout << "I forget" << endl;};
	void Disk() 	{ cout << "Space" << endl;};
	void Erased() 	{ cout << "For good" << endl;};
	void ThisExam() 	{ Erased(); MyMemory(); };
};

class IsOver : public Top {
public:
	void MyMemory() 	{ cout << "Gone" << endl;};
	void Disk() 		{ cout << "Slipped" << endl;};
	void virtual Erased() 	{ cout << "Rubbed out" << endl;};
};
void main()
{
	Top* Hat = new IsOver;
	Hat->MyMemory();
	Hat->Disk();
	Hat->ThisExam();

	Top Dog = *(new IsOver);
	Dog.MyMemory();
	Dog.Disk();
	Dog.ThisExam();
}


Mini-Adventure (MA). The game MA consists of a maze of rooms. A room consists of four walls and one or more doors. Doors can be either open, close, or closed and locked. Some locked doors are opened by a key, others are opened by a magic phrase. Each key opens only one door, except for the master key. Doors lead into other rooms. Rooms are rectangular shaped. Long skinny rooms are called hallways. Rooms may contain gold coins, keys, and/or scrolls with magic phrases. When picked up, scrolls self destruct in a fixed amount of time. This time differs for each scroll. The goal of the game is to explore the rooms and collect as much gold as possible. A player can move around using commands as: goUp, goDown, goLeft, goRight, gotoX where X can be Door, key, scroll, and pickUpX where X can be key, scroll, and gold. Each time a game is played a new maze is created and the player starts in a randomly selected room. A player can see on the screen only the contents of the current room and the outline of rooms they have already been in.

2 a. Give a list of potential classes for the MA game.


b. Pick a class from part a. and give a list of responsibilities for the class,
3. Give a detailed scenario for the game MA involving the class in problem 2b.


4. a. What is an abstract class?
b. What is the purpose of abstract classes in object-oriented programming?

5. Define the following terms in object-oriented programming.
a. client-server

b. contracts

c. protocol

6. What is permitted and what is not permitted by the law of Demeter?

7. a. Why do some object-oriented experts recommend programmers avoid case (and if) statements?


b. Give two alternative mechanisms to case (and if) statements.

Final Fall 1993



1) Explain the object-oriented meaning of the following terms.

a) abstraction

b) encapsulation

c) information hiding

2) What problem(s) does multiple inheritance have that single inheritance does not?

3) a) What is polymorphism?

b) Give an example of polymorphism in C++.

4) Why is polymorphism important in object-oriented programing?

5) We have two classes A and B. What do the following relationships indicate about the two classes:

a) A is-a B

b) A has-a B

c) A is-kind-of B

d) A is-analogous-to B

e) A depends-upon B

6) a) What is a scenario?

b) What role(s) do scenarios play in object-oriented design.


7) Identify the classes in the following description of a refrigerator. List at least one responsibility for each class.

A refrigerator has a motor, a temperature sensor, a light and a door. The motor turns on and off primarily as prescribed by the temperature sensor. However, the motor stops when the door is opened. The motor restarts when the door is closed if the temperature is too high. The light is turned on when the door opens and is turned off when the door is closed.

8) a) What is a subsystem?

b) What is the purpose of a subsystem in the Wirfs-Brock object-oriented design process?

c) Why are subsystems found toward the end of the process, rather than at the beginning?


9) What is a contract? Give an example.
10) Briefly describe the steps in the Wirfs-Brock object-oriented design process.
11) What is a virtual function in C++? How does a virtual function operate?
How is inheriting a virtual function different than inheriting a normal functions members?


----------