Exam 1 Study Guide


You MUST present a picture ID

Exam Date:

  • Sections 0101 - 0108 (Mr. Frey) Thursday March 18, 2004
  • Sections 0201 - 0208 (Mr. Raouf) Wednesday March 17, 2004

    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. You are also responsible for all associated reading assignments and material covered in lab.

    Answering the "self-test" questions in each chapter of the text is a good way to review.


      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. Why is it necessary for the last line of main to be "return 0;"?
    3. What is the name of the standard input stream in C++?
    4. What is the name of ths standard output stream in C++?
    5. TRUE/FALSE
      1. Variables can be declared almost anywhere in the code
      2. In a for loop a variable can be declared at the same time it's initialized as in for (int i = 0; i < 42; i++).
      3. C++ supports the bool data type, but C does not.
      4. To use cout/cin, you must include the header file named iostream.
      5. Strings and vectors are built-in data types.

      Functions

    6. Why is function overloading possible in C++, but not in C?
    7. What is a function's "signature"?
    8. Explain the differences between the function prototypes below. When would each prototype be appropriate?
      void GetFirstCharacter ( string s);
      void GetFirstCharacter ( string& s);
      void GetFirstCharacter ( const string& s);
    9. 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.
    10. In many cases, the use of default parameter values is an alternative to function overloading. Give an example in which either method can be used. Discuss why you might choose one method over the other.
    11. Explain how the compiler decides which function to call when given several functions with the same name.
    12. What is the cause of this typical compiler error? BadSqrtPow.cpp: In function `int main()': BadSqrtPow.cpp:10: call of overloaded `xxxx( int )' is ambiguous
    13. List the four basic ways that parameters can be passed.
    14. Explain how "call by reference" in C++ is similar and different than passing parameters with pointers in C.
    15. Explain when each method of parameter passing is appropriate for built-in types like int and double.
    16. Explain when each method of parameter passing is appropriate for user defined objects.
    17. Can a const object invoke a non-const member function? If so why? If not, why not?
    18. Can a non-const object invoke a non-const member function? If so why? If not, why not?
    19. Can a const object be passed as an argument by reference to a function? If so why? If not, why not?
    20. Can a non-const object be passed as an argument by reference to a function? If so why? If not, why not?
    21. Define PreConditon, PostCondition as used in function header comments required by our coding standards.
    22. Explain three ways in which functions might handle unstatisfied PreConditions.
    23. What do we mean when we say that parameters and variables declared inside a function are "local" variables.
    24. Do Self-Test exercise 25 on page 126 of the text to test your understanding of variable scope.
    25. What is the scope of a variable declared in a 'for' loop control statement?
      (i.e. for (int i = 0; i < size; i++)
    26. What is the output from the following code? Is it correct? If not, how can it be fixed to print out the correct output? // arrange n1 and n2 in sorted order void Arrange(int n1, int n2); int main ( ) { int low = 44; int high = 33; Arrange (low, high); cout << "Lowest = " << low << endl; cout << "Highest = " << high << endl; return 0; } void Arrange (int n1, int n2) { if (n1 > n2) { int temp; temp = n1; n1 = n2; n2 = temp; } }

      Strings and Vectors

    27. 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
    28. Explain the difference between using the input operator >> and using the function getline( ) for reading strings input by the user.
    29. Describe the potential problem when using both getline( ) and the input operator >> to read strings and integers. How is this problem addressed?
    30. 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.
    31. Give at least three ways in which strings are superior to C-style char arrays used as strings.

    32. Write the variable declaration for a vector of strings named text
    33. Explain the difference between these two declarations
      vector< int > v1( 10 );
      vector< int > v2[ 10 ];
    34. Explain the difference between a vector's size and its capacity.
    35. Explain the difference between using brackets and the function at() to access individual elements of an array. When is each appropriate?
    36. In what ways are vectors like arrays?
    37. In what ways are vectors different from arrays?
    38. 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.

      Classes and Objects

    39. The concept of a constructor may be confusing because it is a member function of a class which is rarely explictly called. Explain when a constructor is called.
    40. Define "default constructor".
    41. Explain the difference between a class and an object Use a "real life" entity as an example.
    42. Explain the OO design technique of "aggregation". Explain how aggregation promotes code reuse.
    43. When using aggregation, some accessors will have a return type of "reference to const". When is this return type appropriate for an accessor? Why is this return type used in this case?

      The following questions refer to the following class definition

      class Car { public: Car( int cylinders = 4, int passengers = 5, const string& color = "Red"); void Start( ); void Stop( ); void TurnLeft( ); void TurnRight ( ); const string& GetColor ( ) const; int GetCylinders( ) const; int GetNrPassangers( ) 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; };
    44. Write the code to implement Car's constructor.
    45. In how many ways may Car's constructor be invoked? List them.
    46. Write the code to implement Car's SetNrPassengers() method.
    47. What is the significance of const in the prototype for GetCylinders()
    48. What is the significance of const string& as the return type in the protoype of GetColor().
    49. Write the declarations and code to instantiate a Car object for a Black, 6-cylinder car that can hold two people. Write the code to start the Car.
    50. What is the significance of int cylinders = 4 as the parameter for SetCylinders()
    51. Under what circumstances can a user of Car directly access m_color?
    52. Identify the use of "aggregation" in the definition of Car (if any).

    53. Define: member access specifier, object, instance, instantiation, class scope, user, implementer, mutator, accessor, function signature
    54. 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.
    55. 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 ( ) const; const Point& GetCenter( ) const; int GetY ( ) const; int GetRadius( ) 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 center.
      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;
      6. Assume that you are asked to write the function PrintPoint( const Point& point ); that prints a Point object in the format ( x, y ). Would you choose to make PrintPoint() a friend of the Point class above or not? Justify your answer.
      7. If you were required to write PrintPoint() as a friend of the Point class, where would the prototype for PrintPoint() be found? Write the prototype for PrintPoint() if it were a friend of Point.
    56. What does it mean for a function to be a friend of a class?
    57. Some people argue that the use of friendship violates the principles of encapsulation. What do you think? Justify your position.
    58. Explain what we mean when we say that static data members are "restricted global variables".
    59. True/False - Static data members of a class are shared among all objects of that class.
    60. True/False - Static functions of a class can only access static data members of that class.
    61. True/False - Every member function of a class can access the static data members of that class.
    62. True/False - Static data members are declared and intialized outside the class.
    63. True/False - Static data members may not be const.

      Operator Overloading

      Use the class definition of Fraction for the questions below class Fraction { public: Fraction (int numerator = 1, int denominator = 1); int GetNumerator( ) const; int GetDenominator( ) const; // other public methods??? private: int m_numerator; int m_denominator; };
    64. Write the prototype for operator<< for Fraction.
    65. Write the code for operator<< assuming operator<< IS NOT a friend of Fraction.
    66. Write the code for operator<< assuming operator<< IS a friend of Fraction.
    67. Explain why operator<< CANNOT be a member function of Fraction.
    68. Write the prototype for operator+ that adds two Fractions and returns a Fraction as a result, if operator+ is a member function of Fraction.
    69. 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.
    70. Which version of operator+ is most useful? Explain your answer.
    71. Write the prototype for the unary negation operator (operator-) for Fraction.
    72. Write the prototype for the pre-increment operator of class Fraction if is declared as a friend function.
    73. What functions must Fraction support if overloaded operators are written as non-member, non-friend functions?

      Dynamic memory allocation

    74. Define "memory leak".
    75. 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];
    76. 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;
    77. Explain what happens in the following new and delete expressions Car *parkingLot = new Car[50]; delete [] parkingLot;

      Copy Constructor and Assignment Operator

    78. Why must the parameter to the copy constructor be passed by (const) reference?
    79. Write the prototype for the copy constructor of a class named AB123YZ.
    80. 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";
    81. Under what condition(s) will the default copy constructor supplied by the compiler lead to memory leaks or runtime errors?
    82. What is the purpose of the test if (this != &rhs) in the code for operator= (where rhs is the parameter)?
    83. Why does the assignment operator always return *this?
    84. Define deep copy, shallow copy
    85. Write the copy constructor for the Car class above.
    86. Write the assignment operator for the Car class above.