Computer Science 2610

Spring 2000

Quiz 1’

 

 

 

  1. A contract for a function should be sure to discuss

(a)   why the function was written.

(b)  where the parameters will come from.

(c)   what the function’s parameters mean to what the function computes.

(d)  the purposes of the local variables.

 

  1. A computer program should be broken up into small functions because

(a)   doing so makes it possible to avoid using global variables.

(b)  doing so makes the program easier to write.

(c)   doing so makes the program run much faster.

(d)  doing so makes the program more confusing.

 

  1. Which of the following is NOT a standard type in C++?

(a)   char

(b)  double

(c)   long

(d)  function

 

  1. Function four is shown below.  It is known that four prints something when it runs.  What does it print?  (Hint: before starting a hand simulation, look at the structure of the function.)

(a)   0

(b)  1

(c)   2

(d)  9

 

void four()

{

  int k = 9;

  while(k != 1) {

    if(k % 2 == 0) k = k / 2;

    else k = 3*k + 1;

  }

  cout << k << endl;

}

 


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

(a)   4

(b)  5

(c)   16

(d)  65

 

 

void five()

{

  int i,k;

  i = 1;

  k = 1;

  while(i < 4) {

    k = k * i + 1;

    i++;

  }

  cout << k << endl;

}

 

  1. 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 = 90;

  y = x – x / 2;

  z = x / 4;

  w = x % 4;

  b = y > x;

  cout << “x = “  << x << “ y = “ << y << “ z = “ << z

       << “ w = “ << w << “ b = “ << b << endl;

}

 

 

  1. The average of two numbers is half their sum.  Write a C++ function called average that returns the average of its two parameters.

 

            double average(double x, double y)

     {