CS 1005, C Term 2000
Introduction to Programming
Solutions to Test 2

Your Name: _____________________________________

Your Lab Section: ______________     Your Login Name:_________________

Instructions.   Read each problem carefully before answering. Circle the selected answer(s) or write your solutions in ink in the spaces provided when this is the appropriate action as indicated below for each problem. Write neatly. Each problem is worth 60 points. All parts of each problem have equal point value. C++ is the programming language used throughout this test. Good luck!

  1. a) Among the following collection of terms, circle the three that correspond to the three basic types of public member functions that are required for most C++ classes:
    Arrows are used instead of circles here for typesetting reasons...
    
    -->Modification member functions     Vector indices
    
       Loop tests                        Constant member functions <--
    
       Private data members              Constructors <--         
    
       Homogeneous functions             Destructors
    
    

    b) A class named VolumeControl has exactly six public member functions, named VolumeControl(), TurnUpANotch(), TurnDownANotch(), TurnOff(), GetVolume(), and IsOn(). Assume that p, q, and r are three objects of type VolumeControl. Circle all statements among those shown below that would be syntactically incorrect if used within a client program of the VolumeControl class. You may assume that the source file that contains these statements provides access to the VolumeControl class through the appropriate #include "volume_control.h" directive.

    
    -->q*TurnUpANotch();                 TurnDownANotch = p; <--
    
       p.TurnOff();                      VolumeControl z;
    
    -->p.q;                              z = VolumeControl; <--
    
    
    
    
    
    
    
    
    
    
    

  2. a) For each of the following, write one or more C++ statements that would accomplish the given objectives within a client program of the vector class. All variables used must be explicitly declared unless otherwise stated.
       Declare a vector named thisvector that contains 10 int objects.
    
          vector<int> thisvector(10);
    
    
       Place the size of a vector named thisvector in an object named thisroll
       (no need to declare thisvector).
    
          int thisroll = thisvector.size();
    
    
       Assign the value 5 to the item in position 4 of the vector
       named thisvector (no need to declare thisvector).
    
          thisvector[4] = 5;
    
    

    b) Assume that the size() method (member function) of the vector class has the following prototype:

       int size() const;
       // returns the number of items in the current vector object
    
    Answer each of the following:
       Is the size() method a constructor, a modifier, or a selector?
       
          Answer: selector (because of the const keyword)
    
    
       Give the header only (no function body required) of the size method
       exactly as it would appear in the implementation file vector.cpp:
    
          int vector::size() const
    

  3. Give the output of each of the following program segments.

    a)

       vector<double> row(5);        
       if (row.size()!=1) {
          cout << "kerplunk";
       }
       else {
          cout << "yahoo";
       }
       cout << " " << row.size();
    
       Answer: kerplunk 5
    

    b)

       vector<int> thisvector(4);
       for (int i=0; i<thisvector.size(); i++) {
          thisvector[i] = i*i;
       }
       for (int i=0; i<thisvector.size(); i++) {
          cout << thisvector[3-i];
       }
    
       Answer: 9410
    

  4. Circle the correct answer in each of the following cases.

    a) A struct is a class that has

    
       Only constant members
    
       At least two modifier methods
    
    -->Only public members
    
    

    b) Vectors satisfy all of the following except which one?

    
    -->The time necessary to access an item of a vector depends on its position
    
       Vectors are homogeneous aggregates
    
       Vector items are indexed by integers
    
    

    c) The correct declaration for a 6-sided Dice object named box is:

    
       Dice(6) box;
    
    -->Dice box(6);
    
       Dice box.(6);
    
    

  5. Given the function named UFO (Unidentified Function Object) defined below:
       int UFO(vector<int> & v)
       {
          int pos = 0;
          while ( (pos < v.size()) && (v[pos] != 0) ) {
             v[pos] = -1;
             pos++;
          }
          return pos;
       }
    
    
    Assume that myvector is a vector containing 10 ints, with myvector[i] equal to 10-i for each i between 0 and 9.

    a) Give the value returned by the call

       UFO(myvector);
    
       answer: 10
    

    b) What output is produced by the following program segment?

       for (int i=0; i<10; i++) {
          myvector[i] = 10 - i;
       }
       int count = UFO(myvector);
       cout << count << endl;
       for (int i=0; i<10; i++) {
          cout << myvector[i] << " ";
       }
    
       Answer: 10
               -1 -1 -1 -1 -1 -1 -1 -1 -1 -1