CSCI 3510
Spring 2004
Practice questions for quiz 4

  1. True or false?

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

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

    3. A hash table is an excellent tool to help to sort an array.

    4. All data structures have a logical size that is determined when the program is compiled, and cannot change while the program runs.

    5. You are allowed to add an integer to a pointer in C++.

  2. Suppose that the following structure definition is given.

       struct Widget
       {
         int side;
         char* front;
         char bottom[22];
         Widget(int s, char* f, char *b)
         {
           side = s;
           front = f;
           strcpy(bottom, b);
         }
       };
    
    1. Show how to create a new widget called frodo with side = 4, front = "red" and bottom = "blue". Use the constructor.

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

  3. The following C++ program compiles without errors, but when it is run, it causes a memory fault. Why?

        #include <iostream>
        using namespace std;
        int main()
        {
          char* word;
          cin >> word;
          cout << word << endl;
          return 0;
        }
    

  4. The following function is supposed to test whether two strings are the same, provided upper and lower case letters are treated as equal. That is, 'a' and 'A' should be considered the same letter. It is supposed to return 1 if the two strings are the same (when case is ignored) and 0 when they are not the same. For example, sameUpper("abc", "ABC") = 1, but sameUpper("abc", "AbD") = 0. This function works by copying each string, replacing lower case letters by upper case letters, and then using library function strcmp to tell whether the resulting strings are the same. (strcmp(s,t) returns 0 if strings s and t are equal, a negative number if s comes before t in alphabetical order, and a positive number if s comes after t in alphabetical order.) The function also uses function toupper, which converts a lower case letter to upper case, and returns all other characters unchanged. So toupper('a') = 'A', toupper('A') = 'A' and toupper(':') = ':'. There is a serious mistake in this function. Explain what the mistake is, and rewrite the function to avoid the mistake. You might need to make more than one change, but try to keep the spirit of the method instead of choosing a completely different method. Make sure the external behavior of the function is correct.

        bool sameUpper(char* x, char* y)
        {
          char *cpyx, *cpyy;
          int xlen = strlen(x);
          int ylen = strlen(y);
          int i;
          
          if(xlen != ylen) return 0;
          
          for(i = 0; i <= xlen; i++) {
            cpyx[i] = toupper(x[i]);
            cpyy[i] = toupper(y[i]);
          }
          
          if(strcmp(cpyx, cpyy) == 0) return 1;
          else return 0;      
        }
    

  5. Write a function called stutter that takes a null-terminated string s as a parameter and produces, in newly allocated memory, the string that results from repeating each character in s twice. For example, stutter("abaac") = "aabbaaaacc". Stutter should return a pointer to the memory where the new string is stored.

  6. What is the purpose of a destructor for a class? What is the destructor for class River called? Under what circumstances is the destructor called?