Final Exam, CSCI 330, Fall 2020


  1. [4 points] What is a namespace and what problems do namespaces solve when implementing a large project?

  2. [6 points] Name or describe three methods by which class member data can be initialized. When, if ever, is the use of one of these methods preferred or required.

  3. Friends and Encapsulation
    1. [2 points] What is a friend function?
    2. [4 points] Provide an argument against employing friend functions liberally when implementing classes.

  4. Class Members
    1. [3 points] Describe the unique properties of static member functions.
    2. [3 points] Describe the unique properties of static member data.

  5. [4 points] The operator [] can be overloaded in two ways. Describe each. Describe the difference between the two routines.

  6. [5 points] What will be the result of executing the following code. There are no intentional syntax errors. Assume all member functions are correctly implemented elsewhere.
    class PersonT {
        public: 
           PersonT()=default;
           void SetName(string s);
           void SetAge(int i);
           string GetName();
           int GetAge();
        private:
           int age;
           string name;
    };
    
    PersonT & PersonFactory() {
        PersonT data;
        data.SetName("Unknown");
        data.SetAge(0);
        return data;
    }
    
    int main() {
         PersonT & person = PersonFactory();
         cout <<  person.GetName() << " is " << person.GetAge() << endl;
       return 0;
    }
    

  7. Consider the following code
    class Base {
        public:
            Base()=default;
            virtual ~Base()=default;
            void PrintClass() { cout << data;}
            void SetData(int d) {data = d;}
        private:
            int data;
    };
    
    class Derived: public Base {
         public:
             void PrintClass();
             void SetData(int d){Base::SetData(d);}
             void ChangeData(int d) {data = d;}
    };
    
    int main() {
        Base * item = new Derived;
        item->ChangeData(x);
        item->PrintClass();
        return 0;
    } 
    1. [4 points] There are two non-syntax reasons that this code will not compile. What are they? (Identify the line where the error occurs and describe the error)
    2. [4 points] How can each error be corrected.

  8. [5 points] Describe the problem caused by the following code. How can this problem be corrected? (Again, there are no intentional syntax errors)
    class Base {
        public:
            virtual ~Base() = default;
            ...
        private:
            int baseData[MAX_DATA_SIZE];
    };
    
    class Derived1: public Base {
      ...
    };
    
    class Derived2: public Base {
      ...
    };
    
    class Mixed: public Derived1, public Derived2 {
      ...
    }; 

  9. [6 points] Provide the code for a templated function Mode that takes a vector of elements and returns the item occurring most frequently. If multiple items occur with equal maximum frequency, return the one with the minimum value.

Submission Instructions