CS 176, Introduction to Computer Science II

Summer 2001 - Final Exam, Part 2


  1. Use the following phrase triples

    1.a reference parameter,a copy of the value,reference to the original value,
    2.automatically invoked,goes out of scope,a destructor,
    3.const modifier,a parameter declaration,not change the value,
    4.dangling pointer,a pointer,deallocated memory,
    5.header file,a program,an #include statement,
    6.ifstream and ofstream,define streams,manipulate files,
    7.initialize,an object,declared,
    8.iostream library,manipulators,stream insertions and extractions,
    9.not been deallocated,no pointer points,a memory leak,
    10.reused,the declarations,occur in different scopes,
    11.sequence of characters,a string object,a terminating '\0' character,

    to complete the following sentences

    1. A __________ is __________ that points to __________.

    2. A __________ is incorporated into __________ using __________.

    3. A member function that is __________ when an object __________ is called __________.

    4. Always __________ __________ when it is __________.

    5. Dynamic memory that has __________, but to which __________ is called __________.

    6. Names can be __________ as long as __________ associated with the names __________.

    7. The __________ defines __________ for specifying how to perform __________.

    8. The __________ that constitute __________ does not include __________.

    9. The classes __________ __________ that __________.

    10. __________ passes a __________ instead of a __________..

    Each underscore __________ represents a phrase; phrases are assigned to blanks in the left-to-right order given in the triple. Each triple is used at most once; note that there is one more triple than sentences (that is, there will be one triple set that doesn't get used).


    1. A dangling pointer is a pointer that points to deallocated memory. (triple 4)

    2. A header file is incorporated into a program using an #include statement. (triple 5)

    3. A member function that is automatically invoked when an object goes out of scope is called a destructor. (triple 2)

    4. Always initialize an object when it is declared. (triple 7)

    5. Dynamic memory that has not been deallocated, but to which no pointer points is called a memory leak. (triple 9)

    6. Names can be reused as long as the declarations associated with the names occur in different scopes. (triple 10)

    7. The iostream library defines manipulators for specifying how to perform stream insertions and extractions. (triple 8)

    8. The sequence of characters that constitute a string object does not include a terminating '\0' character. (triple 11)

    9. The classes ifstream and ofstream define streams that manipulate files. (triple 6)

    10. a reference parameter passes a reference to the original value instead of(a copy of the value). (triple 1)


  2. You are reading a colleague's program and you see the following code line:
    i = object.method1().method2();
    
    Using no other information than what you see in this code line, write down two facts about method1()'s return type.


    The first fact is that object.method1() must return an object (class or struct) because it's used as the left operand of a dot accessor:

    ( object.method1() ) . method2()
    
    The second fact is the object returned by object.method1() must contain a method function named method2() because that's the right operand of the dot accessor.


  3. Given the declaration
    int * ip = new int(1);
    
    a colleague of yours is always forgetting which of the statements
    delete ip;
    
    or
    delete *ip;
    
    is correct. Recognizing your C++ expertise, your colleague asks you for help. Which of these statements is correct, and what tip can you give you colleague to help remember which statement is correct?


    The tip your colleage should remember that delete always deletes values of type pointer; it is syntatically incorrect to delete a value of any other type.

    Using this tip, your colleague's quandry becomes easy to resolve: ip is a value of type pointer (to int), so delete ip is correct. *ip is a value of type int, which is not a pointer (pace C++'s idiot implicit conversions), so delete *ip is incorrect.


  4. You are teaching a C++ course at a summer computer camp, and you have given your students a test that requires them to write a procedure the prints a 3x3 identity matrix. One of your students has written the following procedure:
    void print_ident(void) {
      int x = 3
      int ident[x, x] = { 0 };
      for (int i = 1; i <= 3; i++)
        ident[i, i] = 1;
      for (int i = 1; i <= 3; i++) {
        for (int j = 1; i <= 3; j++)
          cout << ident[i, j] << " ";
        cout << '\n';
        }
      }
    
    Grade this function. Don't forget to explain why you marked down the program.


    Alas, practically every line in this program is wrong:

    void print_ident(void) {
      int x = 3			  // no semicolon
      int ident[x, x] = { 0 };	  // syntax is [x][x]; x is not a constant.
      for (int i = 1; i <= 3; i++)	  // index range is 0 <= i < 3.
        ident[i, i] = 1;		  // [,] syntax again
      for (int i = 1; i <= 3; i++) {  // index range again
        for (int j = 1; i <= 3; j++)  // index range again
          cout << ident[i, j] << " "; // [,] syntax again
        cout << '\n';		  // ok
        }
      }
    



This page last modified on 27 August 2001.