Computer Science 2610
Fall 2000
Solutions to practice questions for quiz 4

  1. If Node is a type, then expression new Node returns a value of type
    1. Node
    2. Node*
    3. Node**
    4. Node&

  2. The C++ keyword used to return memory to the pool of available memory is
    1. new
    2. free
    3. release
    4. delete

  3. Which of the following will copy the null-terminated string that is in array src into array dest?
    1. dest = src;
    2. dest == src;
    3. strcpy(dest, src);
    4. strcpy(src, dest);

  4. A pointer is a memory address. Suppose that pointer variable p holds address 1000, and that p is declared to have type int*. Suppose that an int is 4 bytes long. What address is represented by expression p+2?
    1. 1002
    2. 1004
    3. 1008
    4. None. This expression is not allowed.

  5. Which of the following allocates a new array A in the heap, holding 10 bytes?
    1. char A[10];
    2. int A[10];
    3. char* A = new char[10];
    4. int* A = new int[10];

  6. Which of the following is true if null-terminated strings A and B are the same string?
    1. strcmp(A,B) != 0
    2. strcmp(A,B) == 0
    3. A = B
    4. A == B

  7. What is the tail of list [6,2]?
    1. 6
    2. 2
    3. [6]
    4. [2]

  8. What is printed by the function fourteen when it is run?
         void fourteen()
         {
            int* s;
            int* p = new int;
            int* q = p;
            int* r = new int;
            *p = 8;
            s = p;
            *q = 25;
            *r = *s;
            q = r;
            cout << "*p = " << *p << endl;
            cout << "*q = " << *q << endl;
            cout << "*r = " << *r << endl;
            cout << "*s = " << *s << endl;
         }
    

      *p = 25
      *q = 25
      *r = 25
      *s = 25
    

    Remark. It is very important for this kind of problem not to cut corners. Draw a picture! Show the pointers. Do not try to avoid using pointers. Do not be sloppy. If you cut corners or are sloppy, you will get the wrong answer.

    Some students will ignore this advice. They will get the wrong answer.

  9. Write a function that returns a copy of a null-terminated string. The copy should be put into memory that is allocated using new, and it should be null-terminated. Return a pointer to the new memory.

      char* copy(char *s)
      {
        char* cp = new char[strlen(s) + 1];
        strcpy(cp, s);
        return cp;
      }
    

  10. Using the list type and operations presented in class (type List and operations head, tail, cons and emptyList) write a function allPositive(L) that returns 1 if all members of list L are positive numbers, and returns 0 otherwise.

    Here are two solutions, one using recursion and the other using a loop.

    Note that allPositive([]) should return 1, since there are no non-positive members of [].

      bool allPositive(List L)
      {
        if(L == emptyList) return 1;
        else if(head(L) <= 0) return 0;
        else return allPositive(tail(L));
      }
    

      bool allPositive(List L)
      {
        List p = L;
    
        while(p != emptyList) {
          if(head(p) <= 0) return 0;
          p = tail(p);
        }
        return 1;
      }