CSCI 2610
Spring 2000
Practice questions for quiz 2

The actual quiz will probably be a little shorter than this. These questions are a sample of the kind of questions that you will be asked to answer.

  1. Which of the following is an invariant for the loop inside function one? (Hint: Look at the values that the variables n and m take on.)
    1. n > 0
    2. m = 14
    3. 2n+m = 14
    4. n = n - 1
       int one()
       {
         int n,m;
         n = 4;
         m = 6;
         while(n > 0) {
           n = n - 1;
           m = m + 2;
         }
         return m;
       }
    

  2. Given the following definition of function f, what are the values of f(1), f(f(1)) and f(f(f(1)))?
       int f(int x)
       {
         if(x < 2) {
           return x + 1;
         }
         else {
           return 2*x + 1;
         }
       }
    

  3. If function t is called, what is printed. Be very careful with this question.
       void r(int a, int x, int &z)
       {
         cout << r: a = " << a << " x = " << x << " z = " << z << endl;
         a = 2;
         x = 5;
         z++;
       }
    
       void t()
       {
         int a, b, x, z;
         a = 9;
         b = 20;
         x = 67;
         z = 92;
         r(a,b,x);
         r(a,b,x);
         cout << "t: a = " << a << " b = " << b
              << " x = " << x << " z = " << z << endl;
       }
    

  4. What is the value of mystery(4)?
       int mystery(int n)
       {
         if(n == 0) return 0;
         else return mystery(n-1) + n;
       }
    

  5. Write a recursive function sevens so that sevens(n) returns 1 if n has any sevens in its decimal representation, and returns 0 otherwise. For example, sevens(3701) = 1, sevens(777) = 1 and sevens(42) = 0. Assume that n is not negative.

    Note that you can get the rightmost digit of a number n by taking the remainder when you divide n by 10, and you can remove the rightmost digit by dividing n by 10.

    For this problem, do not use any form of loop, and do not alter the value of any variable that already has a meaningful value.

       int sevens(long n)