Computer Science 2610
Summer 2000
Solutions to practice questions for exam 1

Answers are in bold blue. Remarks on the answers are in 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.

    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

    7. The word if in C++ is usually used to start a loop. F The word if starts a conditional, not a loop.

  2. 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. (You have not seen breaks yet.) 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.

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

    NOTE: There was an error in this problem -- it did not give an initial value to m. So m had a junk value in it. I have corrected the error here, setting m to 0 initially.

    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.

  4. The following sets variable x to
    1. 2
    2. 3
    3. 4
    4. 5
        int x,y,z;
        y = 4;
        z = y - 1;
        if(y > z || z < 0) x = z - 1;
        else x = z + 1;
    

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

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

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

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