CSCI 3510
Spring 2004
Practice questions for quiz 3

  1. True or false.

    1. If Node is a type, then expression new Node returns a pointer of type Node*.

    2. A dangling pointer is pointer to memory that has been deleted.

    3. Storing a value at a memory address that is a dangling pointer can cause some minor problems, but the program usually recovers from them quickly.

    4. If a C++ program does not return memory to the free space pool when it is done with the memory, then the system will periodically sweep through memory and return unused memory to the free space pool automatically.

  2. If p has type T* then what is the type of *p?

  3. C++ notation p->x abbreviates

    1. (p*).x
    2. (*p).x
    3. (p.x)*
    4. p.(*x)

  4. Suppose that you have allocated a widget as follows.

        Widget* p = new Widget;
      
    If you compute with the widget for a while and then want to recycle the memory occupied by this widget, which of the following statements should you use?
    1. delete p;
    2. delete *p;
    3. delete Widget p;
    4. delete Widget *p;

  5. What does the following function print when it is run?

         void test()
         {
           int* s;
           int* p = new int;
           int* q = p;
           int* r = new int;
           *r = 17;
           s = r;
           *r = 41;
           *q = *s;
           p = s;
           *r = 8;
           r = q;
           cout << "*p = " << *p << endl;
           cout << "*q = " << *q << endl;
           cout << "*r = " << *r << endl;
           cout << "*s = " << *s << endl;
         }
    

  6. Suppose that a linked list is built from list cells, where each cell has the following type.

        struct ListCell
        {
          ListCell* next;
          int item;
          ListCell(int i, ListCell* n)
          {
            next = n;
            item = i;
          }
        };
      
    Write a function that will compute the sum of all of the numbers in a linked list. The sum of an empty list is defined to by 0. A heading is given.
          int sum(ListCell* L)