CSCI 3300
Spring 2010
Exercises for Quiz 2

  1. What is the value of g(4), given the definition of g below? (Hint. Work out g(1), then g(2), then g(3), then g(4), in that order. Keep your work organized.)

      int g(int n)
      {
        if(n == 1) return 2;
        else return g(n-1) + 3;
      }
    

    Answer

  2. Write a C++ definition of function sum(a,b) that returns a + (a+1) + ... + b. For example, sum(2,5) = 2 + 3 + 4 + 5 = 14. More precisely, sum(a,b) returns the sum of all integers that greater than or equal to a and less than or equal to b. For this question do not use any kind of loop. Use recursion instead.

    Answer

  3. We discussed the distinction between two areas of memory: the heap and the run-time stack. Suppose that variable p has type int*. Which of the following is a correct C++ statement or statement sequence that makes p point to newly allocated memory in the heap?

    1. *p = new int;
    2. p = new int;
    3. p = new int*;
    4. int x; p = &x;
    5. int x; *p = x;

    Answer

  4. Suppose that p is defined to point to memory as in the preceding question. Which of the following stores 25 in the integer variable to which p points.

    1. p = 25;
    2. p* = 25;
    3. *p = 25;
    4. p = *25;
    5. p *= 25

    Answer

  5. What is the value of variable x after performing the following sequence of statements?

      int x = 50;
      int* p = &x;
      *p = 4;
    

    Answer

  6. What is the value of variable x after performing the following sequence of statements?

      int y = 7;
      int x = 35;
      int* p = &x;
      p = &y;
    

    Answer

  7. What does jump( ) return, where jump is writtten below, using function jumpHelper?

     void jumpHelper(int x)
     {
       x = x + 1;
     }
    
     void jump()
     {
       int z = 40;
       jumpHelper(z);
       return z;
     }
    

    Answer

  8. What does hop( ) return, where hop is writtten below, using function hopHelper?

     void hopHelper(int& x)
     {
       x = x + 1;
     }
    
     void hop()
     {
       int z = 40;
       hopHelper(z);
       return z;
     }
    

    Answer

  9. What does romp( ) return, where romp is written below, using function rompHelper?

     void rompHelper(int a, int& b)
     {
       b = a + 2;
       a = b + 1;
     }
     int romp()
     {
       int x = 4;
       int y = 25;
       rompHelper(x,y);
       return x + y;
     }
    

    Answer

  10. Which of the following will create an array of 30 integers called Orange in the run-time stack?

    1. int[30] Orange;
    2. int* Orange[30];
    3. int Orange[30];
    4. int[] Orange[30];

    Answer

  11. Which of the following will create an array of 30 integers called Orange in the heap?

    1. int Orange[30];
    2. int* Orange = new int[30];
    3. int[] Orange = new int[30];
    4. new int[30] Orange;
    5. new int Orange[30];

    Answer

  12. Suppose that a C++ program contains the following statements.

      int* p;
      p[0] = 1;
    
    Which of the following is a true statement about what happens?
    1. Performing those statements can cause unpredictable results.
    2. The program containing those statements will get a fatal compile error.
    3. Performing those statements will always lead to a run-time error
    4. There is nothing wrong with those statements; they store 1 into the first cell in an array of integers.

    Answer