Date: Mon, 11 Nov 1996 17:12:27 GMT Server: NCSA/1.5 Content-type: text/html Last-modified: Fri, 04 Oct 1996 07:25:42 GMT Content-length: 2631 Quiz 2 Solutions - CS 302 Fall 1996 - Section 4

CS 302 Fall 1996 - Section 4

Quiz 2 Solutions



This quiz is 20 total points. Please write legibly. If I can't read it, I can't grade it. You have until 5 minutes after the class period ends to finish. Good luck.
  1. (2 points) What does the ``magic'' type ``const char *'' allow us to do?

    It allows us to pass a constant character string to a function.

  2. (2 points) State one difference between a local variable and a call-by-value parameter?

    call-by-value parameters have an initial value obtained from an actual parameter from the call to the funciton. Local variables start with an unspecified initial value and thus they need to be assigned or explicity initialized before used.

  3. (2 points) What does a void keyword at the beginning of a function header or prototype mean?

    That function returns no value.

  4. (2 points) Based on what was said in class, when are ``global'' variables acceptible to use?

    I accepted two answers: Never or only when they are global constants.

  5. (2 points) Give one reason to use a call-by-reference(&) parameter.

    A function needs to give back more information than a single variable.

  6. (5 points) What is the output of the following program?
    #include<iostream.h>
    
    void height(int total_inches, int& inches, int& feet);
    
    int main() 
    {
      int my_height = 78;
      int height_inches, height_feet;
    
      height(my_height, height_inches, height_feet);
      cout << "I am " << height_feet << " feet, ";
      cout << height_inches << " inches tall." << endl;
    }
    
    void height(int total_inches, int& inches, int& feet)
    {
      inches = total_inches % 12;
      feet = total_inches / 12;
    }
    

    I am 6 feet, 6 inches tall.

  7. (5 points) Exercise 7, page 184: Write a void function definition for a function called add_tax. The function add_tax has two formal parameters: tax_rate which is the amount of sales tax expressed as a percentage and cost which is the cost of an item before tax. The function changes the value of cost so that it includes sales tax.
    void add_tax(int tax_rate, int& cost)
    {
      cost = (1.0 + tax_rate) * cost;
    }