Exam 1 Study Guide


Picture ID is REQUIRED for all exams

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. Be sure to check out any student exercises found in the lecture notes.

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

For all TRUE/FALSE questions below which are FALSE, explain why.


    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 the standard output stream in C++?
  5. What is argc? What is argv? What is their purpose?
  6. 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.
    6. argv[0] is always the name of your executable program.

    Streams

  7. Define "stream". What are some advantages of using streams?
  8. Define cout, cerr and cin
  9. Write the code to create a stream named inFile, attach it to a file named "input.txt", and then open the stream.
  10. 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.
  11. Describe two ways to check for EOF when reading a file.
  12. 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;
  13. What is an I/O manipulator?
  14. Explain the effect of each manipulator -- fixed, showpoint, left, right, setprecision, and setw
  15. What is the output from these statements? double x = 42; cout << fixed << showpoint << setprecision( 4 ) << x << endl;
  16. 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;

    Functions and member functions

  17. Why is function overloading possible in C++, but not in C?
  18. What is a function's "signature"?
  19. Explain the differences between the function prototypes below. When would each prototype be appropriate?
    char GetFirstCharacter ( string s );
    char GetFirstCharacter ( string& s );
    char GetFirstCharacter ( const string& s );
    void GetFirstCharacter ( const string& s, char& c);
  20. 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.
  21. 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.
  22. Why does this attempt at defining default parameter values result in a compiler error/warning? Vacation( int startMonth = 1 , int startDay, int duration = 0);
  23. Explain how the compiler decides which function to call when given several functions with the same name.
  24. 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
  25. List the four basic ways that parameters can be passed to a function.
  26. Explain how "call by reference" in C++ is similar and different than passing parameters with pointers in C.
  27. Explain when each method of parameter passing is appropriate for built-in types like int and double.
  28. Explain when each method of parameter passing is appropriate for user defined objects.
  29. Can a const object invoke a non-const member function? If so why? If not, why not?
  30. Can a non-const object invoke a non-const member function? If so why? If not, why not?
  31. Can a const object be passed as an argument by reference to a function? If so why? If not, why not?
  32. Can a non-const object be passed as an argument by reference to a function? If so why? If not, why not?
  33. Define PreConditon, PostCondition as used in function header comments required by our coding standards.
  34. Explain three ways in which functions might handle unsatisfied PreConditions.
  35. What do we mean when we say that parameters and variables declared inside a function are "local" variables.
  36. Do Self-Test exercise 25 on page 126 of the text to test your understanding of variable scope.
  37. What is the scope of a variable declared in a 'for' loop control statement?
    (i.e. for (int i = 0; i < size; i++)
  38. 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

  39. 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.
  40. Explain the difference between using the input operator >> and using the function getline( ) for reading strings input by the user or read from a file.
  41. Describe the potential problem when using both getline( ) and the input operator >> to read strings and integers. How is this problem addressed?
  42. 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.
  43. Give at least three ways in which strings are superior to C-style char arrays used as strings.

  44. Write the variable declaration for a vector of strings named text
  45. Write the variable declaration for a vector of 4 string named sentence in which each string is initialized to "I love C++".
  46. Given that integers is a vector, what compiler error/warning, will result from the statement int nrElements = integers.size( ); Rewrite the statement to eliminate the error/warning.
  47. Explain the difference between these two declarations
    vector< int > v1( 10 );
    vector< int > v2[ 10 ];
  48. Explain the difference between a vector's size and its capacity.
  49. Explain the difference between using brackets and the function at() to access individual elements of a vector. When is each appropriate?
  50. In what ways are vectors like arrays?
  51. In what ways are vectors different from arrays?
  52. List 3 "bad things" about an array and how a vector is better at handling them.
  53. 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

  54. What is a class?
  55. The concept of a constructor may be confusing because it is a member function of a class which is rarely explicitly called. Explain when a constructor is called.
  56. Define "default constructor".
  57. Explain the difference between a class and an object Use a "real life" entity as an example.
  58. Explain the OO design technique of "aggregation". Explain how aggregation promotes code reuse.
  59. 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?
  60. Discuss the pros and cons of having a mutator return a boolean value indicating whether or not it was successful.

    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( ) const; void TurnLeft( ); void TurnRight ( ); const string& GetColor ( ) const; int GetCylinders( ) const; int GetNrPassengers( ) const; void SetColor (const string& color); void SetCylinders (int cylinders = 4); void SetNrPassengers (int passengers); private: int m_cylinders; int m_passengers; string m_color; bool m_isMoving; };
  61. Write the code to implement Car's constructor.
  62. In how many ways may Car's constructor be invoked? List them.
  63. Write the code to implement Car's SetNrPassengers() method.
  64. What is the significance of const in the prototype for GetCylinders()
  65. What is the significance of const string& as the return type in the prototype of GetColor().
  66. 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.
  67. What is the significance of int cylinders = 4 as the parameter for SetCylinders()
  68. Under what circumstances can a user of Car directly access m_color?
  69. Identify the use of "aggregation" in the definition of Car (if any).
  70. Why does the following implementation of Stop result in a compiler error? void Car::Stop( ) const { m_isMoving = false; }

  71. Define: member access specifier, object, instance, instantiation, class scope, user, implementer, mutator, accessor, function signature
  72. 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.
  73. What is the "scope resolution operator" and what is its purpose?
  74. Define "zombie" object and one method for dealing with them.
  75. Which member function(s) has/have the responsibility for determine when an object becomes invalid? Explain why.
  76. What is the purpose of a private member function?
  77. 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 Alter (const Point& newCenter); void Alter (int newRadius); 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 Alter( const Point& newCenter) method.
    2. Explain the relationship of Circle's Alter( ) methods in the context of aggregation.
    3. Write the implementation of Circle's GetCenter() method.
    4. Write the implementation of Circle's constructor with and without using a member initialization list.
    5. Write one line of code to output the x- and y- coordinates of circle2's center.
    6. Write one line of code to make circle2 and circle1 concentric (i.e. have the same center).
    7. Find the errors 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;
    8. 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.
    9. 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.
  78. What does it mean for a function to be a friend of a class?
  79. Some people argue that the use of friendship violates the principles of encapsulation. What do you think? Justify your position.
  80. Explain what we mean when we say that static data members are "restricted global variables".
  81. True/False - Static data members of a class are shared among all objects of that class.
  82. True/False - Static functions of a class can only access static data members of that class.
  83. True/False - Every member function of a class can access the static data members of that class.
  84. True/False - Static data members are declared and initialized outside the class.
  85. 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; };
  86. Write the prototype for operator<< for Fraction.
  87. Write the code for operator<< assuming operator<< IS NOT a friend of Fraction.
  88. Write the code for operator<< assuming operator<< IS a friend of Fraction.
  89. Explain why operator<< CANNOT be a member function of Fraction.
  90. Write the prototype for operator+ that adds two Fractions and returns a Fraction as a result, if operator+ is a member function of Fraction.
  91. 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.
  92. Which version of operator+ is most useful? Explain your answer.
  93. Write the prototype for the unary negation operator (operator-) for Fraction.
  94. Write the prototype for the pre-increment operator of class Fraction if is declared as a friend function.
  95. What functions must Fraction support if overloaded operators are written as non-member, non-friend functions?


Last Modified: Tuesday, 28-Dec-2004 13:45:56 EST