Computer Science 2610
Spring 2003
Solutions to practice questions for exam 1

Answers are in blue. Remarks on the answers are in dark red.

  1. Write a clearly legible T to the left of each of the following statements that is true and a clearly legible F to the left of each that is false. Here, the answers on written to the right of the question for convenience. On the exam, please follow the instructions on placement of the answers.

    1. A function contract usually cannot be written until after the function is written. F Function contracts are written before the functions are written, since otherwise you don't know what the problem is that is to be solved.

    2. A function typically is given its data through its parameters. T

    3. In order for a function to get any data to work on, it must include a "cin" line. F Some functions talk to the user. But most functions get data to work on through parameters, not from the user.

    4. The data type of real numbers is called real in C++. F It is called double.

    5. C++ uses 0 to mean "false" and 1 to mean "true". T

    6. C++ is a professional software development language. T

  2. A loop invariant is

    1. An assertion about the values of the program's variables that is true about those values each time the program reaches the top of a given loop.
    2. A statement about how the variables of a loop are modified when the loop body runs.
    3. A kind of loop that has a special syntax in C++.
    4. A way of initializing the variables for a loop.

  3. Function f2 is shown below, with part of its definition not shown, and replaced by three dots. There are no breaks in the part that is not shown, and nothing is printed there. It is known that f2 prints something when it runs. What does it print?

    1. 24
    2. 25
    3. 26
    4. It is impossible to say without more information.
          void f2()
          {
            int i = 100;
            while(i != 25) {
              ...
              i = i + 1;
            }
            cout << i;    
          }
    

    The only place to print something is after getting out of the loop, where i is printed. But the only way to get out of the loop is to find that i = 25.

  4. The following loop sets variable m to

    1. 1 + ... + 9
    2. 1 + ... + 10
    3. 1 + ... + 11
    4. 1 + ... + 12
         int m,n;
         n = 0;
         m = 0;
         while(n < 10) {
           n++;
           m = m + n;
         }
    

    Notice that the last time through the loop, the loop body is started with n = 9, since n = 10 will cause the loop to exit. The loop body adds 1 to n, making it 10, and then adds 10 to m. So the largest value added into m is 10.

  5. What is the value of C++ expression 5+9*8-6?

    1. 28
    2. 71
    3. 83
    4. 106

  6. What is printed by the following C++ function when it is run? Be careful to note that all of the variables hold integers.

           void four()
           {
             int x,y,z,w,b;
             x = 45;
             y = 2 * x + x * 3;
             z = x / 2;
             w = x % 2;
             b = x < y;
             cout << "x = " << x << " y = " << y << " z = " << z
    		   << " w = " << w << " b = " << b << endl;
           }
    

    Answer: x = 45 y = 225 z = 22 w = 1 b = 1

  7. The geometric mean of two numbers x and y is the square root of the product of x and y. Write a C++ function called geometricMean that returns the geometric mean of its two parameters.

                double geometricMean(double x, double y)  
                {
                  return sqrt(x*y);
                }
    

  8. Using function geometricMean from the previous exercise, write a program fragment that sets variable z to the geometric mean of variable r and twice variable s.

       z = geometricMean(r, 2*s);
    

  9. If function t is called, what is printed? Be very careful with this question.

    	void r(int x, int y, int& z)
    	{
    	  cout << "r: x = " << x << " y = " << y 
                << " z = " << z << endl;
    	  x = x + 1;
    	  z = y + x;
    	}
    	 
    	void t()
    	{
    	  int a, b, x;
    	  a = 20;
    	  b = 25;
    	  x = 250;
    	  r(a,b,x);
    	  r(x,a,b);
    	  cout << "t: a = " << a << " b = " << b 
    		 << " x = " << x << endl;
         }
    

    Answer:

             r: x = 20 y = 25 z = 250
             r: x = 46 y = 20 z = 25
             t: a = 20 b = 67 x = 46
    

    The key to getting this right is to do the hand simulation carefully. Be sure to remember how to handle call-by-reference and call-by-value. Before the first call to r, the picture looks like this.

             t
         ----------
           a = 20
           b = 25
           x = 250
    

    Now r(a,b,x) is called. The picture looks like this.

             t                    r
         ----------           ---------
           a = 20              x = 20
           b = 25              y = 25
           x = 250 <-          z .
                     |___________|
    
    When function r runs, it prints line
             r: x = 20 y = 25 z = 250
    
    and then performs the two assignments. The picture looks like this.
             t                    r
         ----------           ---------
           a = 20              x = 21
           b = 25              y = 25
           x = 46  <-          z .
                     |___________|
    
    Now r returns, and t is about to call r again. The picture is like this.
             t
         ----------
           a = 20
           b = 25
           x = 46
    
    Now t calls r(x,a,b). The picture looks like this just before r runs.
             t                    r
         ----------           ---------
           a = 20              x = 46
           b = 25  <-          y = 20
           x = 46    |         z .
                     |___________|
    
    Now r runs. It prints line
             r: x = 46 y = 20 z = 25
    
    Then r does the two assignments. The picture is like this.
             t                    r
         ----------           ---------
           a = 20              x = 47
           b = 67  <-          y = 20
           x = 46    |         z .
                     |___________|
    
    Now r returns, leaving t ready to print its line. It prints
             t: a = 20 b = 67 x = 46
    

  10. 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
    
    

    Do a careful hand simulation. Since f(3) = 4, the second part is to compute f(4). Don't bother to compute f(3) again; you know it is 4.

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

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

    You can find out the value of mystery(4) by computing mystery(1), mystery(2) and mystery(3) first.

       mystery(1) = 1
       mystery(2) = 3*mystery(1) = 3*1 = 3
       mystery(3) = 3*mystery(2) = 3*3 = 9
       mystery(4) = 3*mystery(3) = 3*9 = 27
    
    The key to getting this kind of question right is to stay organized, and not to be sloppy.

    Answer: 27

  12. Write a recursive function anySevens(n) that returns the 1 if there are any 7's in the decimal representation of n, and 0 otherwise. For example, anySevens(974) = 1, anySevens(7) = 1 and anySevens(32) = 0. Function anySevens should require its parameter n to be nonnegative. 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.

  13.     bool anySevens(int n)
        {
          if(n == 0) return 0;
          else if(n % 10 == 7) return 1;
          else return anySevens(n/10);
        }
    

    Clearly, 0 has no 7's in it. If the rightmost digit of n is(n%10) is 7, then n does have a 7 in it. If n is not 0 and the rightmost digit of n is not 7 then n has a 7 just when all but the rightmost digit of n (n/10) has a 7. For example, to test n = 2743, you would notice that the rightmost digit is not 7, so you would give the same answer as you would give on input 274. If you can't stand the idea that a boolean value is really a value, then another way to write this function is as follows.

        bool anySevens(int n)
        {
          if(n == 0) return 0;
          else if(n % 10 == 7) return 1;
          else if(anySevens(n/10)) return 1;
          else return 0;
        }