CMSC 202 Spring 2004
Final Exam Study Guide

You MUST present a picture ID

Exam Dates
Sections 0101 - 0108 (Frey) Thursday May 13, 2004 10:30am - 12:30pm
Sections 0201 - 0208 (Raouf) Wednesday May 12, 2004 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 midterm exam. However, by its very nature, this programming course is cumulative and so to some degree the final will also be cumulative but will emphasize the material presented after the midterm. You are also responsible for all associated reading assignments. Be sure to check out any "student exercises" found in the lecture notes.

    I. Streams

  1. Define "stream". What are some advantages of using streams?
  2. Define cout, cerr and cin
  3. Write the code to create a stream named inFile, attach it to a file named "input.txt", and then open the stream.
  4. Given a file that contains an unspecified number of integers separated by white space, write the code that reads all the integers from the file, adds them to a running sum, prints the sum and closes the file.
  5. Describe two ways to check for EOF when reading a file.
  6. What is the output from the following code segment (assuming it's embedded in a complete and correct program).
    The file "input.dat" contains this single line of data: 1 23 4 567 8 90 ifstream in( "input.dat" ); int count = 0; int item; while ( in >> item ) { ++count; cout << item << endl; } cout << count << endl;
  7. What is an I/O manipulator?
  8. Explain the effect of each manipulator -- fixed, showpoint, left, right, setprecision, and setw
  9. What is the output from these statements? double x = 42; cout << fixed << showpoint << setprecision( 4 ) << x << endl;
  10. What output will be produced when the following lines of code are executed? cout << "*" << setw( 5 ) << 123; cout << left; cout << "*" << setw( 5 ) << 123; cout << right; cout << "*" << setw( 5 ) << 123; cout << endl;

    II. Inheritance and Polymorphism

  11. Explain the differences among public, private, and protected member access specifiers. Be sure to mention their role in inheritance.
  12. Explain how inheritance promotes code reuse.
  13. Explain the difference between the "is a" and "uses a" relationships. Give an example of each from the course projects.
  14. Explain how C++ implements the "is a" relationship between objects.
  15. Explain how C++ implements the "has a" relationship between objects.
  16. Describe the order in which constructors and destructors are called when using inheritance.
  17. Explain the use of the member initialization list for constructors.
  18. Explain the difference between function overriding and function overloading.
  19. In terms of interface and implementation, what is the purpose of a pure virtual method in a base class?
  20. In terms of interface and implementation, what is the purpose of a virtual method in a base class?
  21. In terms of interface and implementation, what is the purpose of a non-virtual method in a base class?
  22. Explain the difference between static (early) and dynamic(late) binding.
  23. Define abstract base class. Give an example from one of the course projects.
  24. Why should destructors be virtual in a base class?
  25. Does polymorphism work through several layers of inheritance?
  26. Although overriding a non-virtual function is permitted, it's not a good idea. Explain why this is true.

    True/False

  27. A base class contains all data and methods which are common to all objects in its inheritance hierarchy.
  28. In C++, public inheritance is used to support the "uses-a" relationship between objects.
  29. Aggregation (composition) is used to support the "has-a" relationship between objects.
  30. Like friends, a derived class has direct access to the base class' private data and methods.
  31. Dynamic binding is performed at run-time when the programmer uses pointers to functions.
  32. 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.
  33. The base class destructor is automatically called when a derived class object is destroyed.
  34. Polymorphism can only occur when using indirect access (pointers or references ) to derived objects.
  35. Polymorphism can only occur when using virtual or pure virtual methods.
  36. 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.
  37. 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.
  38. Polymorphism refers to how identical code can produce different effects depending on the actual type of the object being processed.
  39. Class member functions may not be declared "protected".
  40. The base class assignment operator is automatically called when a derived class assignment operator is used.
  41. The base class default constructor is automatically called when a derived class constructor is used unless a different base class constructor is specified in the member initialization list.
    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 ( ); virtual void Print ( ) const; int GetWheels ( ) const; void SetWheels (int wheels ); private: int m_nrWheels; }; //------------------------------------ Vehicle::Vehicle (int wheels) { m_nrWheels = wheels; } //------------------------------------- int Vehicle::GetWheels ( ) const { return m_nrWheels; } void Vehicle::SetWheels (int wheels) { m_nrWheels = wheels; } void Vehicle::Print ( ) const { cout << "Vehicle"; } Vehicle::~Vehicle ( ) { // no code }
    // Parking Lot class const int maxVehicles = 20; class Lot { public: Lot ( ); int NrParked ( ) const; void Park ( Vehicle& v ); int TotalWheels ( ) const; void Print( ) const; private: int m_nrVehicles; Vehicle *m_vehicles[maxVehicles]; }; //-------------------------------- Lot::Lot ( ) { m_nrVehicles = 0; } //---------------------------------- int Lot::NrParked ( ) const { return m_nrVehicles; } //---------------------------------- void Lot::Park (const Vehicle& vehicle) { m_vehicles[nrVehicles++] = &vehicle; } //---------------------------------- void Lot::Print ( ) const { for (int v = 0; v < nrParked(); v++) { (m_vehicles[v])->Print( ); cout << ":"; cout << (m_vehicles[v])->GetWheels(); cout << endl; } } //--------------------------------- int Lot::TotalWheels ( ) 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 ( ); }; //--------------------------------- MotorCycle::MotorCycle (int wheels) : Vehicle (wheels) { // no code } //=================== // Car class class Car : public Vehicle { public: Car (int wheels = 4); void Print ( ) const; }; //------------------------------ Car::Car (int wheels) : Vehicle (wheels) { // no code } void Car::Print ( ) const { cout << "Car"; }

    The following questions refer to the code above

  42. What two features of this code tell you that dynamic binding is taking place? Be specific.
  43. What is the output from main() ?
  44. Write the copy constructor for the Motorcycle class.
  45. Write the assignment operator for the Motorcycle class.
  46. Write the destructor for the Motorcycle class.
  47. True/False -- The Vehicle class is an abstract class.
  48. True/False -- Motorcycle is derived from Vehicle.
  49. True/False -- Car is derived from Vehicle.
  50. True/False -- The Lot class is not intended to be a base class.
  51. True/False -- The compiler will prevent the Car class from overriding Vehicle's GetWheels() method.
  52. True/False -- The compiler will not prevent the Car class from overriding Vehicle's GetWheels() method, but it shouldn't do so.
  53. True/False -- SetWheels() cannot be used polymorphically because it's not a virtual or pure virtual function.
  54. True/False -- TotalWheels() is a polymorphic function.

    III. Templates

  55. 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 );
  56. 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.
  57. 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.
  58. 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.
  59. 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.
  60. What attribute(s) of a function makes it appropriate to be a function template?
  61. Write a template class definition for a class named Box that contains homogenous data and supports the following operations. The capacity 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 }
  62. 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 argument.
  63. Implement the member function operator[] for the MyArray class above. Throw an exception of your choosing if you detect a bad index.

    IV. Exceptions

  64. Name one important advantage of using the C++ exception methodology.
  65. Briefly explain when exception handling should be used.
  66. Briefly explain how exceptions are implemented in C++.
  67. Describe the process of "stack unwinding" which occurs if an exception is not caught.
  68. 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 indicating 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

  69. Exception classes are different from other classes because they can only contain error messages.
  70. Only one try block is allowed in a program.
  71. A try block can have multiple catch blocks.
  72. Exceptions that are not caught by your program result in a run-time error and core dump.
  73. Exceptions should be caught by reference to avoid unnecessary copying.

    V. Containers and Iterators

  74. Define "container class" and give two examples from the STL.
  75. How are container classes implemented in C++?
  76. Define iterator. What is the purpose of an iterator?
  77. Explain the advantages of using iterators for containers.
  78. Why are iterators required for some containers?
  79. Given the code fragment below, write a for-loop using iterators that prints the contents of the vector vector< int > iVector; iVector.push_back( 42 ); iVector.push_back( 22 ); iVector.push_back( 32 ); iVector.push_back( 82 ); iVector.push_back( 62 ); iVector.push_back( 12 ); iVector.push_back( 72 ); // write your for-loop here
  80. How would the for-loop you wrote in the question above be different if the container were a set instead of a vector?
  81. Explain the difference between "constant", and "mutable" iterators.
  82. Explain the difference between "forward" and "reverse" iterators.

    True/False

  83. Some containers handle duplicate elements.
  84. You may instantiate multiple iterators for the same container in the same program/function.
  85. Iterators are created by the container.


Last Modified: Friday, 07-May-2004 10:57:19 EDT