Computer Science 3300
Fall 2005
Practice Questions for Quiz 1

These questions are representative of the kinds of questions that you might get on the quiz. There are more questions here than there will be on the quiz.

This will be a closed book quiz. But you can use one prepared 8.5x11 page of notes, written on both sides, during the quiz.

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

    1. A null-terminated string of length 30 occupies 30 bytes.

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

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

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

    5. Most dynamic storage allocation is done in the static area of memory.

  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)     = _________
    
    
       f(f(3)) = _________
    
    

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

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

    Answer:______________________________________________

  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;
      }
    

  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];

  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];

  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;
          }
      

  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);
         }