CS250 Second Test: Fall 1998 

Student ID: ________________________ Printed Name: _______________________________

Honor Pledge: I pledge that I have neither given nor received unauthorized help in taking this exam. __________________________________

 

You may use blank paper to answer: BUT label each problem clearly and put your student ID on every page handed in. All questions 10 points except 1 (8 points) and 7 (12 points)

1. (Version 4: Doctor Scheduling) A) In which function is the array of doctors allocated?
main (4 points)

   B) Is it allocated as a local variable or on the heap?
local variable (4 points)

2. (Version 4: Doctor Scheduling) You are to change the Doctor Scheduler so that the list of doctors is stored in a template list class. The private data member of the scheduler class shown below
Doctor * doctors;  // old specifications
has been changed to the following
TList<Doctor> doctors; // new specification

Modify the implementation of the function ChooseDoctor to use this new doctor list. You may use either of the two template list classes given in notes 15 (which (un)fortunately are both named TList). 

// solution using first TList class
int Scheduler::ChooseDoctor() const
{
    int n = 0;
    cout << "Please choose one of the following doctor's by number:\n";
    doctors.Head(); // 3 points
    for(int i = 1; i <= doctors.Length(); i++) { // 3 points
        cout << i << ": " << (doctors.Retrieve()).GetName() << endl; // 2 points
        doctors++; // increment position in list - 2points
    }
    do {
        cin >> n;
        n = n - 1; // adjust to array indices
        cin.ignore(255,'\n');
    } while((n < 0) || (n > doctors.Length()));           
    return n;
}


// solution using second TList class
int Scheduler::ChooseDoctor() const
{
    int n = 0;
    cout << "Please choose one of the following doctor's by number:\n";
    if(!doctors.IsEmpty() {
       int i = 1; // 1 point
       doctors.GoTop(); // 3 points
       do {
          cout << i++ /* 1 point */ << ": " << (doctors.Get()).GetName() << endl; // 2 points
       while(doctors.Advance()); // 3 points
    }
    do {
        cin >> n;
        n = n - 1; // adjust to array indices
        cin.ignore(255,'\n');
    } while((n < 0) || (n > i));           
    return n;
}
// source code to working program here

3. (Notes 15) Using the first list object (called List), write a regular function (not a member nor a friend) to insert an item at the end of the list.

void insertEnd(ItemType stuff, List & theList) // 3 points
{
   theList.SetPosition(theList.Length()); // 4 points
   theList.Insert(stuff); // 3 points
}

4. (Notes 11: Student3.h) Write the implementation to overload the "<" operator for Student objects, using the GPA to rank students. Don't make it a friend function.

bool operator <(const Student & S1, const Student & S2) // 4 points
{
   return S1.GetGPA() < S2.GetGPA(); // 6 points
}

5. (Notes 12: LongArray) In the copy constructor for LongArray,

LongArray::LongArray( const LongArray & L )
{
  size = L.size;
  initv = L.initv;
  data = new long[size];
  for(unsigned i = 0; i < size; i++)
    data[i] = L.data[i];
}

   A) what would be the effect of removing the following line of code.

	 data = new long[size];

"data" will be undefined and when it is used later in the loop, unusual things may happen (5 points)
-3 if only says no memory allocated - and not the consequent

   B) What would be the effect of changing this line to:

	 data = L.data;

Shallow copy (the loop later on is irrelevant - it just copies the array onto itself.) (5 points)

6. (Notes 12: VString) A) Next to each line, give the prototype of the function (from the VString class definition) that is used (one line only/no comments).

  VString S1("string 1"); // VString( const char * s ); (1 point)
  VString S2(S1); // VString(const Vstring & v); (1 point)
  S2 = S1; // there is none - uses automatically created overload assignment (2 point)

   B) List all the bad results of executing these statements.

Shallow Copy (3 points)
Memory Leak -- memory allocated to S2 is lost (3 points)
Destructor will fail on second of S1 and S2 destroyed since both point to same memory (3 Points)

7. In notes 12 describing access specifiers, an example was given with ClassA, ClassB and ClassC.

Which is(are) the base class(es)? _____ClassA

Which is(are) the derived class(es)? ______ClassB and ClassC

 

Yes

No

Does ClassB have access to VarA3 XXXXXXXXXXXXXX  
Does ClassC have access to VarA3 XXXXXXXXXXXXXX  
Does ClassB have access to VarA2   XXXXXXXXXXXX
Does ClassC have access to VarA2   XXXXXXXXXXXX
Does ClassB have access to VarA1 XXXXXXXXXXXXXX  
Does ClassC have access to VarA1 XXXXXXXXXXXXXX  
Does ClassB have access to VarC1 (VarC1 public) XXXXXXXXXXXXXX  
Does ClassC have access to VarB2   XXXXXXXXXXXX
Does ClassA have access to VarB2   XXXXXXXXXXXX

8. (Notes 12: LongArray)Write a regular function (not a member, not a friend) that will return the average of all elements in a LongArray.

long average(LongArray theArray) { // 2 points
   long ave = 0;  // 2 points (must set initial value)
   for(int i = 0; i < theArray.GetSize(); i++) { // 2 GetSize
      ave += theArray.Get(i); // 2 Get
   }
   return ave/theArray.GetSize(); // 2 correct computation of average
}

9. (Notes 13) In SafeArray, assume you have made GrowBy a private member function. Modify Put to grow the array, if necessary, using GrowBy.

template <class T>
void SafeArray<T>::Put( unsigned i, T elt )
{
  if( i >= size ){ // 3 points
     GrowBy(i-size+1); // 4 points
  }
  data[i] = elt;
} // 3 points in leaving rest alone

10. (Notes 14) Given the following code for deleting a node in a linked list.

Prev->Next = Cur->Next; // splices out current node
Cur->Next = NULL; // Defensive programming
delete Cur;
Cur = NULL; // Defensive programming

A) what would be the effect of switching the first two lines?
drops end of list beyond cur - memory leak (3 points)
B) what would be the effect of deleting the third line?
memory leak (3 points)
C) when deleting the last node in a linked list, what would be the value of "Cur"? of "Prev"?
Prev points to last node (2 points)
Cur is NULL ( 2 points)