Exam 1 Study Guide


You MUST present a picture ID

Exam Date:

  • Sections 0101 - 0104 (Mr. Frey) Tuesday, March 11, 2003
  • Sections 0201 - 0204 (Mr. Raouf) Monday, March 10, 2003

    Use the list of questions below as a guide when studying for Exam 1. It is by no means a comprehensive list of questions.

    You are responsible for all material presented in lecture up through and including copy constructors and assignment operator.

    You are also responsible for all associated reading assignments and material covered in discussion/lab.

    A wise student won't forget the suggested exercises.


    General C++ Coding

    1. Write C++ code to output the sentence "I am the Greatest" to the standard output device, one word per line.
    2. Write a single line of code to instantiate an ifstream object named infile, associate with a file named "myfile.txt" that contains integers separated by whitespace, and open the file.
    3. Assuming that infile was successfully opened in the question above, write the declarations and code needed to read three (3) integers from the file and insert them into the first 3 elements of a vector named IntVector.
    4. Again assuming that infile from above was opened successfully, write the while loop control structure that reads all the integers from the file one at a time until end-of-file.
    5. Explain the differences between the function prototypes below
      void GetFirstCharacter ( string s);
      void GetFirstCharacter ( string& s);
      void GetFirstCharacter ( const string& s);
    6. Why is function overloading possible in C++, but not in C?
    7. The concepts of constructor, destructor may be confusing because they are member functions of a class which are never explictly called. Explain when a constructor is called. Explain when a destructor is called.
    8. Define PreConditon, PostCondition as used in function header comments required by our coding standards.
    9. Explain three ways in which functions might handle unstatisfied PreConditions.
    10. Define cout, cerr and cin

    Strings and Vectors

    1. Given the declaration string aString = "the quick brown fox jumped over the lazy dog"; write syntactically correct C++ code to
      1. Output the number of characters in aString to the standard output stream
      2. Output the string backwards to the standard output stream
      3. Instantiate a string object named anotherString as a copy of aString
    2. Write the function
      bool FindSubString(const string& string1, const string& string2);
      that returns true if string2 is a substring of string1; false otherwise. Write this function without the use of any string "find" methods.
    3. Give at least three ways in which strings are superior to C-style char arrays used as strings.
    4. Write the variable declaration for a vector of strings named text
    5. Explain the difference between these two declarations
      vector< int > v1( 10 );
      vector< int > v2[10];
    6. In what ways are vectors like arrays?
    7. In what ways are vectors different from arrays?
    8. Given the declaration     vector< int > vInt;
      write C++ code that will store 2, 4, 6, 8, 10 in the vector in that order and then print the contents of the vector.

    Object-Oriented Programming

    1. Explain the difference between a class and an object Use "real life" entity as an example.

      The following 10 questions refer to the following class definition

      class Car { public: Car( int cylinders = 4, int passengers = 5, const string& color = "Red"); ~Car ( void ); void Start( void ); void Stop( void ); void TurnLeft( void ); void TurnRight ( void ); const string& GetColor ( void ) const; int GetCylinders( void ) const; int GetNrPassangers( void ) const; void SetColor (const string & color); void SetCylinders (int cylinders = 4); void SetNrPassageners ( int passengers); private: int m_cylynders; int m_passengers; string m_color; };
    2. Write the code to implement Car's constructor.
    3. In how many ways may Car's constructor be invoked? List them.
    4. Write the code to implement Car's SetNrPassengers() method.
    5. Write the code to implement Car's SetNrPassengers() method using the pointer known as "this".
    6. What is the significance of const in the prototype for GetCylinders()
    7. What is the significance of const string& as the return type in the protoype of GetColor().
    8. Write the declarations and code to instantiate a Car object for a Black, 6-cylinder car that can hold two people and then start the Car.
    9. What is the significance of int cylinders = 4 as the parameter for SetCylinders()
    10. Under what circumstances can a user of Car directly access m_color?
    11. Identify the use of "aggregation" in the definition of Car (if any).
    12. Define: member access specifier, object, instance, instantiation, class scope, user, implementer, mutator, accessor, function signature
    13. What is the purpose of the variable this?
    14. Explain how the C++ features of private data members, public methods, and public data members help or hinder the OO objectives of encapsulation and data hiding.
    15. Explain the differences among the procedural, modular, and object-oriented abstraction models.
    16. Define ADT (abstract data type). In particular, tell what and ADT is and what it is not.
    17. You have been asked to write a set of four functions that find the smallest element in a vector of ints, a vector of doubles, a vector of chars and a vector of strings. You decide to use function overloading. Write the prototypes for these four overloaded functions.
    18. Can a const object be passed to non-const member function? If so why? If not, why not?
    19. Can a non-const object be passed to non-const member function? If so why? If not, why not?
    20. Can a const object be passed as an argument by reference to a function? If so why? If not, why not?
    21. Can a non-const object be passed as an argument by reference to a function? If so why? If not, why not?
    22. What is the significance of a function being declared a friend of a class?
    23. What is the significance of a class being declared a friend of another class?
    24. Given the following class definitions class Point class Circle { { public: public: Point( int x = 0, int y = 0); Circle (const Point& p, int radius); int GetX ( void ) const; const Point& GetCenter( void ) const; int GetY ( void ) const; int GetRadius( void ) const; void Move (int deltaX, int deltaY); void Move (int deltaX, int deltaY); private: private: int m_xcoordinate; Point m_center; int m_ycoordinate; int m_radius; }; }; and the declarations Point point(3, 6); Circle circle1 (point, 5); Circle circle2 (Point (1, 2), 3);
      1. Write the implementation of Circle's Move() method.
      2. Write the implementation of Circle's GetCenter() method.
      3. Write one line of code to output the x- and y- coordinates of circle2's x- and y- coordinates to cout.
      4. Write one line of code to make circle2 and circle1 concentric (i.e. have the same center).
      5. Find the errrors in the following statements (if any)
        1. Point p = (1, 3);
        2. Point p2;
        3. Circle c;
        4. cout << circle1.GetX() << endl;
        5. circle1 = circle2;

    Operator Overloading

    Use the class definition of Fraction for the questions below class Fraction { public: Fraction (int numerator = 1, int denominator = 1); int GetNumerator( void ) const; int GetDenominator( void ) const; // other public methods??? private: int m_numerator; int m_denominator; };
    1. Write the prototype for operator<< for Fraction.
    2. Write the code for operator<< assuming operator<< IS NOT a friend of Fraction.
    3. Write the code for operator<< assuming operator<< IS a friend of Fraction.
    4. Explain why operator<< CANNOT be a member function of Fraction.
    5. Write the prototype for operator+ that adds two Fractions and returns a Fraction as a result, if operator+ is a member function of Fraction.
    6. Write the prototype for operator+ that adds two Fractions and returns a Fraction as a result, if operator+ is NOT a member function of Fraction.
    7. Which version of operator+ is most useful? Explain your answer.
    8. Write the prototype for the unary negation operator (operator-) for Fraction.
    9. Write the prototype for the increment operator of class Fraction if is declared as a friend function.

    Dynamic memory allocation

    1. Define "memory leak".
    2. Given the following declarations and code, draw a picture of memory that shows the relationships among the variables after the code has been executed. modified exercise from lecture notes
    3. Given the following statements that allocate dynamic memory, write the corresponding statements to free the memory.
      1. string *p = new string;
      2. string *p = new string[3];
      3. Time *p = new Time(12, 0, 0);
      4. int *p = new int(33);
      5. int *p = new int[33];
    4. Identify and explain how to fix any errors in the following code. Assume the proper #includes and namespace std. int main ( ) { int *p1 = new int (5); int *p2 = p1; cout << "p1 is pointing to the value " << *p2 << endl; delete [] p1; delete p2; return 0;
    5. Explain what happens in the following new and delete expressions Car *parkingLot = new Car[50]; delete [] parkingLot;

    Copy Constructor and Assignment Operator

    1. Why must the parameter to the copy constructor be passed by (const) reference?
    2. Write the prototype for the copy constructor of a class named AB123YZ.
    3. Given the declaration string bob = "bobby"; which of the following invoke the copy constructor?
      1. string mary = bob;
      2. string mary( bob );
      3. string mary = "bob";
    4. Under what condition(s) will the default copy constructor supplied by the compiler lead to memory leaks or runtime errors?
    5. What is the purpose of the test if (this != &rhs) in the code for operator= (where rhs is the parameter)?
    6. Why does the assignment operator always return *this?
    7. Define deep copy, shallow copy
    8. Write a copy constructor for the Car class above.
    9. Write an assignment operator for the Car class above.