CMSC 202 Fall 2003
Final Exam Study Guide

You MUST present a picture ID

Exam Dates
Sections 0101 - 0108 (Frey) Tuesday December 16, 2003 10:30am - 12:30pm
Sections 0201 - 0208 (Raouf) Monday December 15, 2003 6:00pm - 8:00pm

Use the list of questions below as a guide when studying for the final exam. It is by no means a comprehensive list of all material that you are responsible for. In general, you are responsible for the material presented in lecture since the first exam. You are also responsible for all associated reading assignments.

    I. 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<< should not be a member function of Fraction.
  5. Write the prototype for operator* that multiplies two Fractions and returns a Fraction as a result, if operator* is a member function of Fraction.
  6. Write the prototype for operator* that multiplies 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 pre-increment operator of class Fraction if is declared as a friend function.

    II. Dynamic memory allocation

  10. Define "memory leak".
  11. What are the "big 3" and why are they important when your class uses dynamically allocated memory?
  12. 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];
  13. 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;
  14. Explain what happens in the following new and delete expressions Car *parkingLot = new Car[50]; delete [] parkingLot;

    III. Copy Constructor and Assignment Operator

  15. Why must the parameter to the copy constructor be passed by (const) reference?
  16. Write the prototype for the copy constructor of a class named AB123YZ.
  17. 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";
  18. Under what condition(s) will the default copy constructor supplied by the compiler lead to memory leaks or runtime errors?
  19. What is the purpose of the test if (this != &rhs) in the code for operator= (where rhs is the parameter)?
  20. Why does the assignment operator always return *this?
  21. Define deep copy, shallow copy
  22. Write a copy constructor for the Fraction class above.
  23. Write an assignment operator for the Fraction class above.

    IV. Inheritance and Polymorphism

  24. Explain the differences among public, private, and protected member access specifiers. Be sure to mention their role in inheritance.
  25. Explain how inheritance promotes code reuse
  26. Explain the difference between the "is a" and "uses a" relationships. Give an example of each from the course projects.
  27. Explain how C++ implements "is a" relationship
  28. Explain how C++ implements "has a" relationship
  29. Describe the order in which constructors and destructors are called when using inheritance.
  30. Explain the use of the member initialization list for constructors.
  31. Explain the difference between function overriding and function overloading.
  32. In terms of interface and implemenation, what is the purpose of a pure virtual method in a base class?
  33. In terms of interface and implemenation, what is the purpose of a virtual method in a base class?
  34. In terms of interface and implemenation, what is the purpose of a non-virtual method in a base class?
  35. Explain the difference between static (early) and dynamic(late) binding.
  36. Define abstract base class. Give an example from one of the course projects.
  37. List three good "rules of thumbs" to follow when implementing destructors.
  38. Does polymorphism work through several layers of inheritance?
  39. Why should destructors be virtual?

    True/False

  40. A base class contains all data and methods which are common to all objects in its inheritance hierarchy.
  41. In C++, public inheritance is used to support the "uses-a" relationship between objects.
  42. Aggregation (composition) is used to support the "has-a" relationship between objects.
  43. Like friends, a derived class has direct access to the base class' private data and methods.
  44. Dynamic binding is performed at run-time when the programmer uses pointers to functions.
  45. Because only virtual and pure virtual methods should be overridden, all public methods in a base class should be declared as either virtual or pure virtual.
  46. The base class destructor is automatically called when a derived class object is destroyed.
  47. Polymorphism can only occur when using indirect access (pointers or references ) to derived objects.
  48. Polymorphism can only occur when using virtual or pure virtual methods.
  49. To invoke a virtual function for an object, your code must use a pointer of the same type as the type of the object when it was originally declared.
  50. If a member function was declared as " virtual void foo() = 0;" then it is not necessary to provide any definition of "foo" in the class.
  51. Polymorphism refers to how identical code can produce different effects depending on the actual type of the object being processed.
  52. Class member functions may be be delcared "protected".
    int main ( ) { Lot ParkingLot; Car chevy; Car camry; MotorCycle harley(3); MotorCycle honda; ParkingLot.park (chevy); ParkingLot.park (honda); ParkingLot.park (harley); ParkingLot.park (camry); ParkingLot.print( ); cout << endl; cout << "The lot has: " << ParkingLot.totalWheels( ) << " wheels" << endl; return 0; } // Vehicle class class Vehicle { public: Vehicle ( int nrWheels = 0 ); virtual ~Vehicle ( void ); virtual void Print ( void ) const; int GetWheels ( void ) const; void SetWheels (int wheels ); private: int m_nrWheels; }; //------------------------------------ Vehicle::Vehicle (int wheels) { m_nrWheels = wheels; } //------------------------------------- int Vehicle::GetWheels (void ) const { return m_nrWheels; } void Vehicle::SetWheels (int wheels) { m_nrWheels = wheels; } void Vehicle::Print ( void ) const { cout << "Vehicle"; } Vehicle::~Vehicle ( void ) { // no code }
    // Parking Lot class const int maxVehicles = 20; class Lot { public: Lot ( void ); int NrParked ( void ) const; void Park ( Vehicle& v ); int TotalWheels ( void ) const; void Print( void ) const; private: int m_nrVehicles; Vehicle *m_vehicles[maxVehicles]; }; //-------------------------------- Lot::Lot ( void ) { m_nrVehicles = 0; } //---------------------------------- int Lot::NrParked (void) const { return m_nrVehicles; } //---------------------------------- void Lot::park (const Vehicle& vehicle) { m_vehicles[nrVehicles++] = &vehicle; } //---------------------------------- void Lot::print (void) const { for (int v = 0; v < nrParked(); v++) { (m_vehicles[v])->Print( ); cout << ":"; cout << (m_vehicles[v])->GetWheels(); cout << endl; } } //--------------------------------- int Lot::TotalWheels ( void ) const { int w = 0; for (int v = 0; v < NrParked(); v++) w += (m_vehicles[v])->GetWheels(); return w; } // MotorCycle class class MotorCycle : public Vehicle { public: MotorCycle (int wheels = 2); ~MotorCycle ( void ); }; //--------------------------------- MotorCycle::MotorCycle (int wheels) : Vehicle (wheels) { // no code } //=================== // Car class class Car : public Vehicle { public: Car (int wheels = 4); void Print (void) const; }; //------------------------------ Car::Car (int wheels) : Vehicle (wheels) { // no code } void Car::Print (void) const { cout << "Car"; }

    The following questions refer to the code above

  53. What two features of this code tell you that dynamic binding is taking place?
  54. What is the output from main() ?
  55. Write the copy constructor for the Motorcycle class.
  56. Write the assignment operator for the Motorcycle class.
  57. Write the destructor for the Motorcycle class.
  58. True/False -- The Vehicle class is an abstract class.
  59. True/False -- Motorcylce is derived from Vehicle.
  60. True/False -- Car is derived from Vehicle.
  61. True/False -- The Lot class is not intended to be a base class.
  62. True/False -- The compiler will prevent the Car class from overriding Vehicle's GetWheels() method.
  63. True/False -- The compiler will not prevent the Car class from overriding Vehicle's GetWheels() method, but it shouldn't do so.
  64. True/False -- SetWheels() cannot be used polymorphically because it's not a virtual or pure virtual function.

    V. Templates

  65. Consider this prototype for a template function: template <class Item> void foo(Item x); Which is the right way to call the foo function with an integer argument i?
    1. foo( i );
    2. foo<int>( i );
    3. foo<Item>( i );
    4. foo(<int> i );
    5. foo(<Item> i );
  66. Consider the following definition: template <class Item> Item max (Item a, Item b) { if (a > b) return a; else return b; } What restrictions are placed on the Item data type for a program that uses the max function?
    1. The Item data type must be either int, double, or float.
    2. The Item data type must be one of the built-in C++ data types.
    3. The Item data type must have a copy constructor and operator> defined.
    4. None of the above restrictions apply.
  67. When should a function be implemented as a template function?
    1. When the data types of the parameters all have copy constructors.
    2. When the function's algorithm is independent of the underlying data type.
    3. When the function is relatively short (usually just one line).
    4. When the function only takes one argument.
  68. Write a template function named Largest( ) that returns the largest object in a vector of homogenous objects passed to it. The return value is the index into the vector at which the largest object was found. If more than one object is "largest", return the index of the first one found.
  69. Write a small code fragment (variable declarations and function call) that shows how Largest( ) would be called from main( ) for a vector of objects of type BOB.
  70. What attribute(s) of a function makes it appropriate to be a function template?
  71. Although function/template classes are designed to be used for any class, why might using a template with some classes cause a compiler error? Give an example.
  72. Write a template class definition for a class named Box that contains homogenous data and supports the following operations. The capicity of the Box cannot change. What design decisions must be considered for these Box operations?
    1. Create a new Box. The capacity of the box is 25 items by default unless another capacity is specified by the user when the box is created.
    2. Puts an item into the Box
    3. Removes an item from the Box
    4. Tells how many items are in the Box
    5. Empties the box

      Given the class template for Box above

    6. Write a declaration for a Box that holds 10 integers
    7. Write a declaration for Box that holds 25 XYZ objects
    8. Write the code for operator<< for the Box class
    9. What syntax error, if any, is present in this implementation for the Box destructor? template <class T> Box::~Box( ) { // code here }
  73. Given the following class: class MyArray { public: MyArray(); ~MyArray(); int &operator[](int k); private: int m_theData[66]; }; Rewrite the definition (do not define the member function and constructors, just declare them) as a template class which takes the type being stored as a template arguments.
  74. Implement the member function operator[] from the MyArray class above. Throw an exception of your choosing if you detect a bad index.

    VI. Exceptions

  75. Name one important advantage of using the C++ exception methodology.
  76. Briefly explain when exception handling should be used.
  77. Briefly explain how exceptions are implemented in C++.
  78. Describe the process of "stack unwinding" which occurs if an exception is not caught.
  79. Rewrite the following function so that any exceptions which are thrown by the functions set(), game(), and match() are caught within the function play(). Your code should print a message inidicating the type of exception caught and then call exit() if any exception is thrown. Assume that match() throws objects of type MatchEx and game() throw objects of type GameEx, both of which are defined elsewhere. Assume that set() throws an integer. Score play( void ) { Score s; while (!match( )) { while (!game( )) { s = set( ); } } return s; }

    True/False

  80. Exception classes are different from other classes because they can only contain error messages.
  81. Only one try block is allowed in a program.
  82. A try block can have multiple catch blocks.
  83. Exceptions that are not caught by your program result in a run-time error and core dump.


Last Modified: Saturday, 06-Dec-2003 16:14:50 EST