CMPT 212
Object-Oriented Applications Design in C++

Fall 1997

Midterm Answers


  1. What is the purpose of function prototypes?

    To allow the compiler to check, in one pass, that each function is being called with the correct number of parameters, with the correct parameter types, and with the correct return value type being expected.


  2. An object is an item that has what three qualities?

    1. an identity which makes it distinct from other objects
    2. properties or attributes
    3. the ability to respond to requests


  3. For each group of items listed below, state whether the group is an example of object membership categorization, object composition categorization, or neither. If the group is an example of object membership categorization, state what the items have in common.


  4. What is the output of this program?
       #include <iostream.h>
         
       class myClass {
          public:
             myClass();
             myClass(const int i);
             myClass(const myClass& c);
             ~myClass();
             myClass& operator=(const myClass& c);
          private:
             int data;
          };
         
       myClass::myClass() {
          data = 3;
          cout << "default constructor: data = " << data << endl;
       }
         
       myClass::myClass(const int i) {
          data = i;
          cout << "int constructor: data = " << data << endl;
       }
         
       myClass::myClass(const myClass& c) {
          data = c.data;
          cout << "myClass constructor: data = " << data << endl;
       }
         
       myClass::~myClass() {
          cout << "destructor: data = " << data << endl;
       }
         
       myClass& myClass::operator=(const myClass& c) {
          data = c.data;
          cout << "assignment operator: data = " << data << endl;
          return *this;
       }
         
       int main() {
         myClass a;
         myClass b(7);
         myClass c = b;
         myClass * d = new myClass(4);
         
         delete d;
         c = a;
         
         return 0;
       }
             
    
       default constructor: data = 3
       int constructor: data = 7
       myClass constructor: data = 7
       int constructor: data = 4
       destructor: data = 4
       assignment operator: data = 3
       destructor: data = 3
       destructor: data = 7
       destructor: data = 3
                      


  5. The following program contains the variables a, b, c, d, e, and f. What is the scope and lifetime of each of these variables?
       #include <stdlib.h>
       #include <iostream.h>
         
       static int a = 6;
       const float b = 5.44;
         
       int calculate(char c = 'W') {
          bool d = true;
          static int e = 0;
         
          if (c != 'W') {
             e++;
             d = (e < 5);
             }
        
          return e;
       }
         
       int main() {
          char * f = NULL;
         
          f = new char('H');
          a = calculate(*f);
          delete f;
         
          return 0;
       }
             
    Scope Lifetime
    a module static
    b module (although global was accepted) static
    c local automatic
    d local automatic
    e local static
    f local automatic (although dynamic was accepted)


  6. For each question, state which statements are true and which are false. (Correct answer = 1 point, incorrect answer = -1/2 point, no answer = 0 points)

    1. Some advantages of separate compilation are:
      Parts of a program can be written in different languages. true
      It makes it easier to check that functions are called with the correct parameters. false
      It makes it easier for different programmers to work on different parts of the program. true

    2. Inline functions
      Recursive functions make good inline functions. false
      Inline functions always make a program smaller. false
      The compiler must always see the function definition of an inline function before it reaches the place where the function is called. true

    3. Constants
      Constant data members must always be initialized in the body of the constructor. false
      Constant member functions must not change the instance for which they are called. true
      A constant instance is just an instance whose data members are all declared with the const keyword. false

    4. Pointers
      An address is a memory location, but a pointer is just a value. false
      If we have these definitions:
         int i = 4;
         const int * p = &i;
                             
      then p must always point to i.
      false
      Any memory which is allocated using new should always be released using delete when it is no longer needed. true


  7. What is the output of this program?
      #include <iostream.h>
         
      char myFunc(int i) {
         cout << "one" << endl;
         return 'B';
      }
         
      int myFunc(float f) {
         cout << "two" << endl;
         return 7;
      }
         
      int myFunc(int i, float f) {
         cout << "three" << endl;
         return 8;
      }
         
      float myFunc(char c, int i = 3) {
         cout << "four" << endl;
         return 3.14;
      }
         
      int main() {
        int   x = 1;
        float y = 2.0;
        char  z = 'A';
      
        cout << myFunc(myFunc(y))    << endl;
        cout << myFunc(myFunc(x))    << endl;
        cout << myFunc(myFunc(x, y)) << endl;
        cout << myFunc(myFunc(z, x)) << endl;
        cout << myFunc(myFunc(z))    << endl;
         
        return 0;
      }
             
    
       two
       one
       B
       one
       four
       3.14
       three
       one
       B
       four
       two
       7
       four
       two
       7
                      




Return to CMPT 212 home page



This page is maintained by simpson@cs.sfu.ca. Last updated on 27 Oct 1997.