Computer Science 3300
Fall 2005
Solutions to Practice Questions for Quiz 1

  1. Which of the following are true, and which are false?

    1. A null-terminated string of length 30 occupies 30 bytes. False. (It occupies 31, since one extra byte is used for the null-terminator.)

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

    3. If Node is a type, then expression new Node[15] returns a pointer of type Node*. True. C++ does not distinguish, in terms of types, between a pointer to one variable or to a whole array of variables.

    4. The heap is an area of memory that can change size while a program runs. True.

    5. Most dynamic storage allocation is done in the static area of memory. False. (It is done in the heap. The static area does not grow. That is why it is called static.)

  2. Function f is defined as follows.

         int f(int x)
         {
           int k = 1;
           while(k <= x) {
             k = k + k;
           }
           return k;
         }
    
    What are f(3) and f(f(3))?
       f(3)    = 4
    
    
       f(f(3)) = 8
    
    

  3. What is the value of mystery(4)?

    	int mystery(int n)
    	{
    	  if(1 == n) return 1;
    	  else return 3*mystery(n-1);
    	}
    

    Answer: 27.
    Note. To solve this, compute

        mystery(1) = 1
        mystery(2) = 3
        mystery(3) = 9
        mystery(4) = 27
    
    That way, for example, when you need to know what mystery(3) is while computing mystery(4), you already know it, from the previous line.

  4. Write a function that computes the sum of all of the numbers in an array of integers. There should be two parameters, the array A and the logical size n of the array. A function heading is provided. Use a loop for this function.

    Answer:

      int sum(int* A, int n)
      {
        int sm = 0;
        int k;
        for(k = 0; k < n; k++) {
          sm = sm + A[k];
        }
        return sm;
      }
    
    Although the question asks you to use a loop, here is a recursive implementation of the same function.
      int sum(int* A, int n)
      {
        if(n == 0) return 0;
        else return A[n-1] + sum(A, n-1);
      }
    

  5. You would like to set variable s to the sum of all of the integers in array Fish, which has 12 members. What statement would you write to use function sum to do the computation?

    Answer:

        s = sum(Fish,12);
    

  6. Write a function that has a single null-terminated string parameter. It should return 1 if the string contains the letter 'b', and 0 if it has no occurrences of the letter 'b'. Call it anyBs. For example, anyBs("bat") = 1, anyBs("rabbit") = 1, but anyBs("dog") = 0.

    Answer:

      bool anyBs(char s[]) 
      {
        for(int k = 0; s[k] != '\0'; k++) {
          if(s[k] == 'b') return 1;
        }
        return 0;
      }
    

  7. Which of the following allocates a new array A in the heap, holding 10 bytes? Remember that local variables of functions are stored in the run-time stack, not in the heap.

    1. char A[10];
    2. int A[10];
    3. char* A = new char[10];
    4. int* A = new int[10];

    Answer: (c). (a) and (b) allocate memory in the run-time stack. (d) allocates 10 integers, or 40 bytes on a 32 bit machine.

  8. Which of the following allocates a new array A in the run-time stack, holding 10 bytes?

    1. char A[10];
    2. int A[10];
    3. char* A = new char[10];
    4. int* A = new int[10];

    Answer: (a).

  9. What is returned by function check?

    1. 30
    2. 50
    3. 100
    4. It is impossible to say from what is given.
          int check()
          {
            int* p = new int;
            *p = 50;
            int* q = p;
            int* r = new int;
            *r = 100;
            *q = *r;
            *r = 30;
            return *p;
          }
      

    Answer: (c). Do a careful hand simulation.

  10. 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;
           printf("*p = %i\n", *p);
           printf("*q = %i\n", *q);
           printf("*r = %i\n", *r);
           printf("*s = %i\n", *s);
         }
    

       *p = 8
       *q = 41
       *r = 41
       *s = 8
    
    The key to getting this right is to do a careful hand simulation. Do not cut corners. Show the pointers, and what they point to.