Computer Sciences 302 Final Exam (20%)

Thursday, December 18, 1997

Instructor: Colby O'Donnell

 
 
 
Name: __________________________________ CS Login Name: ________ Section :________
 


Directions:

 
 
 
Question  Topic  Possible Score
1 Review Terms and Concepts 35   
2 Pointers 18   
3 Dynamic Allocation  10   
4 Dynamic ADTs  17   
5 Instructor  20   
Total  100   
 
 
 
 

1.) Review Terms and Concepts [35 Points]

a.) [10 total, 1 pt each] Circle either true or false for each statement. b.) [9 total] Circle the number before all of the endings that make a true statement. c.) [5 total - 1 pt/blank] Fill the blanks with a word that best matches the context of the sentence.

Review Questions Continued

d.) [5 pts] Explain two significant advantages of object-oriented programming.

OOP has a few distinct advantages over non-OOP languages:

Objects can have unique behavior covered with a common interface. The implementation details of the object can be both hidden and protected from the user of the object. That implmentation can be modified, while the interface remains constant.

Class can inherit properties from parent class, thus reducing the need to reduntantly repeat code common to both classes. Inheritance also facilitates class enhancements without the need to rewrite a lot of code.

e.) [3 pts] What is compiling and why do we need to do it?

Compiling is the process of translating a high-level abstract language which a human easily understands, into a low-level concrete language easily processed by the computer. A prime example is compiling C++ code into executable machine language, as we do with the Visual C++ compiler.

f.) [3 pts] Name and describe two main types of errors encountered when programming.

Compiler Errors:

Programming Errors:

2.) Pointers [18 Points]

a.) [6 total, 1 pt each] Circle either true or false for each statement. b.) [12 total - 4 pts each] Diagram the memory allocations and their contents that result after each code fragment executes. Use boxes for memory allocations and arrows for addresses as done in the text.  Use a question mark if the contents of a memory location is undefined.

3.) Dynamic Allocation [10 Points]

a.) [2 pts] What is the significant advantage of using dynamic memory?

b.) [6 total - 3 pts each] Explain what is wrong with each of the following uses of dynamic memory.

c.) [2 pts] Explain what can go wrong when a class allocates dynamic memory but does not define a copy constructor.

If a copy constructor is not defined by the programmer, then the compiler supplies a default copy contructor, which does a memberwise copy of the object. This is also known as a shallow copy, because when pointers are encountered, only the pointer's value is copied, not the structure pointed at. So if the original object's pointer points to an array, the copied object's pointer will point to that same array. This will cause problems when when object's array is modified, both objects will see the modification. Worse yet, if one object is deleted, allow with its allocated memory, then the other object's array will suddenly disappear, leaving dangling pointers.

4.) Dynamic ADTs [17 Points]

Implement the class named Vector that stores a list of integers. A vector has three data members, max is the maximum number of elements, last is the index of the last used element, and list holds the address of a dynamically allocated array of integers.

 }; a.) [4 pts] Define the constructor for the Vector class having a default size of 512 when a size isn't specified. The vector begins with an empty list. Also fill in the prototype and data members above.

b.) [3 pts] Define the destructor for the Vector class. Also fill in the prototype above.

Dynamic ADTs Continued

c.) [5 pts] Define the copy constructor for the Vector class. Also fill in the prototype above.

d.) [5 pts] Define the << operator for the Vector class. Also fill in the prototype above. This method can be used to output the vector of integers, one per line, to either the screen or a file. Your implementation should also allow chaining (e.g. cout << "vector: " << v << endl; where v is a Vector object).

5.)  Instructor [20 points]

a.) [5 pts] Given below are two pieces of code, one which represents an is-a relationship, and the other which represents a has-a relationship. First, label each piece of code with the type of relationship it represents. Second, briefly explain why relationships are the way you chose them.

class Golly {
  public:
    Golly () ;
    Golly (int size);
    Golly (const Golly & g);
    void Add (Wolly w);
  private:
    Wolly * _wollies;
}

class Wolly {
  public:
    Wolly ();
    Wolly (const Wolly & w)
  private:
    int _nonsense;
}
class Wolly {
  public:
    Wolly ();
    Wolly (const Wolly & w)
  private:
    int _nonsense;
}

class Golly : public Wolly {
  public:
    Golly () ;
    Golly (int size);
    Golly (const Golly & g);
  private:
    int _moreInfo;
}
Relationship: has-a Relationship: is-a
Why: Class Golly contains (has-a) variable of type Wolly. Why: Class Golly is a subclass of Wolly. A Golly is-a Wolly.

a.) [15 pts] Write a complete program, which given its arguments (not including argv[0]), reverses the order of the arguments, and also reverses the characters of each argument itself. The program then prints the new arguments to the screen.

For example, if the program is called reverse, then typing reverse hello to all intelligent life would produce the following output: efil tnegilletni lla ot olleh.

#include <iostream.h>

void swap (char & a, char & b) {
  char tmp = a;
  a = b;
  b = tmp;
}

void swap (char * & a, char * & b) {
  char *tmp = a;
  a = b;
  b = tmp;
}

void reverse (char * str) {
  int len = strlen (str);
  for (int i = 0 ; i < len / 2 ; i++) {
    swap (str[i], str[len-i-1]);
  }
}

int strlen (char *s) {
  int len;
  for (len = 0 ; *s != '\0' ; s++);
  return len;
}

void main (int argc, char * argv[]) {
  // First, reverse the order of arguments
  for (int i = 1 ; i < argc / 2 ; i++) {
    swap (argv[i], argv [argc-i]);
  }

  // Now reverse the characters in each argument
  // and then print the argument
  for (int i = 1 ; i < argc ; i++) {
    reverse (argv[i]);
    if (i > 1) cout << ' ';
    cout << argv[i];
  }
  cout << endl;
}

Scratch Page

No work on this page will be graded but it must be turned in with the exam.