605.404.71 Object-Oriented Programming with C++

Summer 2007

Final Exam


Exam Instructions

This exam is open book. You may use your notes from the course, Stroustrup text, and Jossuttis text. If you use any print or electronic source(s) (e.g., other textbooks, and Internet resources) other than those specifically noted above to generate a solution, you must properly cite the source (see links on class WWW page). You may not communicate with any person, except the instructor, about any aspect of the exam until after the hand-in deadline, even if you have already completed your exam. Moreover, you may not use notes or problem solutions from other times this course has been taught or contact any person from a previous semester about this exam.

When preparing to submit your exam, strictly follow these instructions.

  1. Your exams will be submitted electronically. Please package all your exam solution files in a single ZIP archive.
  2. I will be executing programs (if necessary) on an Intel\WinXP platform. If you need to send code compiled for Unix-like systems please ensure it will function under JHU SunOS 5.10 (student & faculty user account system).
  3. Order your answers according to the order of the test in any file naming or internal file contents (that is, make it readily clear to which question your answer corresponds).
  4. all classes in your solution code must follow guidelines in Stroustrup section 11.7, pg 283 ff and lecture materials on Coplien's orthodox canonical class form idiom.
Programs that do not print/display test input/output or unreadable portions of code will be graded as programs that do not function correctly.

Exams are due July 30, 2007 at Noon, Eastern daylight time. Late exams will not be accepted and a grade of zero will be assigned for the exam unless express consent has been received from Mr. Boon prior to distribution of the exam.

Should you need to reach Mr. Boon outside of office hours, you may phone (301) 606-4115 cell number and leave a message. Email sent to John E. Boon, Jr. will be answered only in the evenings during the exam period

Exam Questions

  1. What are the advantages and disadvantages of each of the following styles providing a Boolean type (keep in mind all of the C++ syntax and semantic implications such as including scope, naming, and conversion when providing your answer) (10 pts):
  2. C++ originally allowed the this pointer to be modifiable. One use was to have user-controlled storage management by assigning directly to the this pointer. The assignment of zero meant that the associated memory could be returned to free store. Explain why this was a bad idea. (10 pts)
  3. Complete class Empty so that the C++ compiler does not automatically generate any methods. Use the most efficient code you can and write the least amount of code you can in solving this problem. (20 pts)
    class Base {
       public:
         ~Base();
    };
    
    class Empty: Base {};
    
    int main() {
       Empty e1;
       Empty e2 = e1;
       e2 = e1;
       Empty *pe2 = &e2;
       const Empty *pe1 = &e1;
    }
    
    
  4. Stroustrup, Chapter 12, manipulates Employee and Manager culminating in examples in section 12.2.6.
    1. Define an abstract class Person that would serve as a parent for Employee. (10 pts)
    2. Define a class Customer that is derived from class Person, and ensure that (10 pts)
      1. objects of class Customer can marry objects of class Employee or class Manager (define the operator + to mean marriage).
      2. objects of class Employee and class Manager cannot marry one another; "company" policy prohibits such marriages
      3. this company does not subscribe to the policy that "marriage is only between a man and a woman".
  5. The following bubble sort does not work correctly.
    //Incorrect bubble sort
    #include <iostream>
       
    void swap (int a, int b)
    {
       int temp = a;
       a = b;
       b = temp;
    };
       
    void bubble (int a[], int size)
    {
       int i, j;
       
       for(i = 0; i != size; ++i)
          for(j = i; j != size; ++j)
            if(a[j] < a[j + 1])
               swap (a[j], a[j + 1]);
    };
       
    int main()
    {
       int t[10] = {9, 4, 6, 4, 5, 9, -3, 1, 0, 12};
     
       bubble(t, 10);
       for (int i = 0; i < 10; ++i)
          std::cout << t[i] << '\t'; 
       std::cout << "\nsorted? " << endl;
       return 0;
    }
    
    
    1. Using pre- and post-conditions, stated as assertions, determine the errors in this program. (5 pts)
    2. Produce a correctly working program containing your final pre- and post-condition assertions. (5 pts)
  6. Consider the class
    
    class base {
       . . .
       public:
          virtual void iam() { cout <<  "base\n"; }
       . . .
    };
    
    
    1. Derive two classes from class base, and for each define iam() to write out the name of the class. (5 pts)
    2. Declare objects of each class, and call iam() from them. (5 pts)
    3. Assign the address of objects of the derived classes to base pointers and call iam() through the pointers. (5 pts)
    4. Remove the virtual keyword from the base class member function, run your code in the previous three steps again, and compare the results obtained with the previous three results obtained. (5 pts)
  7. The following code does not work correctly. Why? Repair it as necessary (10 pts)
    
    #include <iostream>
    
    int f() {
       int input;
       std::cin >> input;
       return input;
    }
    
    int i = f();
    
    int main() {
       std::cout << "The value of i is " << i << std::endl;
    }
    
    

Copyright © 2007 John E. Boon, Jr.