Computer Science 2610

Spring 2000

Quiz 1

 

This is a closed book exam.  Give the best answer that you can to each question. 

 

1. The C++ programming language

(a)   was designed as a convenient vehicle for teaching programming to beginning

       programmers.

 

(b)  should only be used for small programs, since it is unsuitable to larger programs.

 

(c)   is a professional software development language intended to be used by expert

     programmers.

 

  (d) was developed in isolation, and is not similar to any other programming languages.

 

2. A contract of a function

  (a) tells exactly what the function does, and what it requires from its caller.

 

  (b) tells how the function works.

 

  (c) tells where the function fits into the overall solution of a problem.

 

  (d) tells why the function was written.

 

3. Which of the following is a correct statement about function contracts?

(a)   A function contract cannot be written until the function is completed, since you

     cannot know what the function does until it has been written.

 

(b)  A function contract should be written before the function is written.

 

(c)   Although a function contract can be written before the function is written, it is usually  better to wait, because debugging your program will cause you to change what the function does, so writing the contract first is a waste of time.

 

(d)  It is a good idea to wait until your entire program is completed before writing any

       comments in it, including contracts, since the comments simply describe what you did.

 


4. Function f2 is shown below, with part of its definition not shown, and replaced by three dots.  There are no special features, such as breaks or gotos, that are not shown . (We have not discussed these.)  It is known that f2 prints something  at the marked line when it runs.  What does it print at that line?

  (a) 15

  (b) 16

  (c) 20

  (d) It is impossible to say without more information.

 

      void f2()

      {

        int i = 20;

        while(i != 15) {

          ...

          i = i + 1;

        }

        cout << i;     // This line prints something  

      }

 

5. What number is printed by function five when it is run?

(a)   3

(b)  4

(c)   6

(d)  8

 

     void five()

     {

        int i,k;

   i = 0;

   k = 1;

   while(i < 3) {

     i++;

     k = k + k;

        }

        cout << k << endl;

     }

 

 

 


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

 

       void six()

       {

         int x,y,z,w,b;

         x = 41;

         y = x – 16 * 2;

         z = x / 2;

         w = x % 2;

         b = x > y;

         cout << "x = " << x << " y = " << y << " z = " << z

              << " w = " << w << " b = " << b << endl;

       }

 

Answer: ________________________________________________________

 

 

7. Write a C++ function definition for function celsiusToFahrenheit, where celsiusToFahrenheit(t) is the Fahrenheit equivalent of Celsius temperature t.  If t is a temperature given in degrees Celsius, then the equivalent temperature in degrees Fahrenheit is (9/5)t + 32.  Be careful to use correct C++ notation when writing this, and keep in mind that t is of type double.  C++ notation is not the same as standard mathematical notation.

 

     double celsiusToFahrenheit(double t)

     {