Computer Science 2610
Spring 2003
Practice questions for exam 1

This is a closed book exam. You may bring one page of prepared notes. You have 75 minutes.

  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.
    2. A function typically is given its data through its parameters.
    3. In order for a function to get any data to work on, it must include a "cin" line.
    4. The data type of real numbers is called real in C++.
    5. C++ uses 0 to mean "false" and 1 to mean "true".
    6. C++ is a professional software development language.

  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 or returns in the part that is not shown, and nothing is printed there. It is known that f2 prints something at the marked line 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;    // This line prints something.
          }
    

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

  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: ________________________________________________________

  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)  
    
    

  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.

  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: ________________________________________________________

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

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

    Answer:______________________________________________

  12. Write a recursive function anySevens(n) that returns 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.

    Hint. You can get the rightmost digit of n by computing n % 10. You can get all but the rightmost digit by computing n/10. For example, if n is 1974 then n % 10 is 4 and n/10 is 197.