CSCI 3300
Section 001
Fall 2005
Practice questions for quiz 2

  1. True or false.

    1. A dangling pointer is pointer to memory that has been deleted, or that you do not own.

    2. 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.

    3. If a C++ program does not return memory in the heap 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. C++ notation p[k] abbreviates

    1. p + k
    2. p + *k
    3. (*p) + k
    4. *(p + k)
    5. *p + *k

  5. Suppose that you have allocated a Frog as follows.

        Frog* p = new Frog;
    If you compute with the Frog for a while and then want to recycle the memory occupied by this Frog, which of the following statements should you use?

    1. delete p;
    2. delete *p;
    3. delete Frog p;
    4. delete Frog *p;
    5. You should not ever try to delete this memory yourself.

  6. Suppose that you have allocated an array of Frogs as follows.

        Frog* p = new Frog[20];
    If you compute with this array for a while and then want to recycle the memory that it occupies, which of the following statements should you use?

    1. delete p;
    2. delete[] p;
    3. delete Frog[] p;
    4. delete Frog[] *p;
    5. You should not ever try to delete this memory yourself.

  7. Suppose that you have allocated a Frog as follows.

        Frog f;
    If you compute with this Frog for a while and then want to recycle the memory occupied by this Frog, which of the following statements should you use?

    1. delete f;
    2. delete *f;
    3. delete Frog f;
    4. delete Frog *f;
    5. You should not ever try to delete this memory yourself.

  8. Suppose that the following structure definition is given.

       struct Widget
       {
         int side;
         char bottom[22];
         Widget(int s, char* b)
         {
           side = s;
           strcpy(bottom, b);
         }
       };
    (The strcpy function copies a null-terminated string into another array. strcpy(bottom,b) copies b into array bottom.)
    1. Show how to create a new Widget called frodo with side = 4 and bottom = "blue". Use the constructor. Build the Widget in the run-time stack.

    2. Show how to build the same Widget as in (a), but this time in the heap. Make frodop be a pointer to the Widget.

    3. Suppose that the constructor were not provided as part of the Widget structure type. Show how to create the same Widget frodo without using the constructor. Build frodo in the run-time stack.

    4. Build frodop, as in part (b), but assuming that the constructor is not included in the definition of Widget.

  9. 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 definition of 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)

  10. Using the ListCell type from the preceding question, write a function that returns the first positive number in a given list. That is, it looks through the list and returns the first number that it finds that is positive. If there are no positive numbers, it should return 0.

        bool firstPositive(ListCell* A)

  11. Using the ListCell type from the preceding question, write a function that takes two lists, and tells whether the two contain exactly the same numbers, in the same order. It should return true if they are the same, and false if they are different.

        bool sameList(ListCell* A, ListCell* B)