From: mjd@saul.cis.upenn.edu ("[*] The One Who Makes the Clocks Run Down")
Newsgroups: talk.bizarre
Subject: The Exam
Date: 24 Jul 92 15:28:55 GMT
Followup-To: comp.lang.c.d
Organization: Eaters of Wisdom


aaron@space.ualberta.ca (Aaron Humphrey) writes:

>Are you taking a typing class by any chance, mjd?

No.

I am teaching a C programming class.

3.2       5  points

Dafydd Painter, a world-famous nostrilologist, is writing C programs to
help him with his work. In one of them he has the directive

    #define  NUMBER_OF_NOSTRILS  2

     He explains: "This constant represents the number of nostrils
possessed by the typical human being. It appears many places in my
program. I represented the number of nostrils with a manifest constant
so that if the value ever changes, it'll be easy to change the code."

     Of course that's silly, because we don't expect the typical number
of nostrils to change. Nevertheless, there is a good reason for
#define'ing NUMBER_OF_NOSTRILS.  What is it?

[One student said, ``In case the number of nostrils people have changes,
it will be easy to change the code because we'll only have to change the
2 in one place.''  Oy gevalt.]


3.3       5  points

Farina Granville, noted quinquagintaseptologist, tried to write a
function that would set the value of a variable to 57:

  void  set_to_57(int  var)
  {
     var  =  57;
  }

     Of course, it didn't work. You explained to her that in order for
the function to change the value of a variable, it must know where that
variable is, and so Farina will have to pass the address of the variable
she wants to change into the function set_to_57.

    Farina is obstinate: "I don't want to pass in a pointer," she says.
"People shouldn't have to know about pointers to use this simple
function. I'll have the function itself get the address once it's
called." Then she writes this:

  void  set_to_57(int  var)
  {
     int  *p;          /*  Address  of  argument  */

      p  =  &var;
     *p  =  57;
  }

     Does this work? If so, how?  If not, why not?

[I was dismayed by how many students said that this would work.]

3.4       5  points

Kelvin R. Reaumur, a noted temperaturologist, wants to write a function
which accepts a Celsius temperature as an argument and which returns the
equivalent Fahrenheit temperature. The formula for converting from
degrees Celsius to degrees Fahrenheit is:

                                9
		deg F = deg C * -  +  32
                                5

Here is what Dr. Reaumur tried:

  double  celsius_to_fahrenheit(double  c)

  {
     double  f;


     f  =  c  *  (9/5)  +  32;
     return  f;
  }

     First, Dr. Reaumur tested the function by passing in 0, the Celsius
freezing point of water, and it correctly returned 32, the Fahrenheit
freezing point of water. Heartened, he then tried it on 37, which is
normal human body temperature, but the return value was 69, instead of
98.6 as it ought to have been.

     What is wrong with this function? Fix it.

[This next one didn't make it onto the exam]

    Hieronymus Bonghit was never quite himself after he was struck on
the head by a falling wah-wah pedal at last year's Saint Patrick's Day
Parade.  His irrational hatred of the Irish rock band `U2' has increased
ever since last March.  This week he wants to erase all the files on his
system which mention U2.  The first step is to identify these files.
Write a program which reads its input and reports whether or not the
sequence of characters `U2' appeared anywhere within it.

--

                And for to see, and eke for to be seye
Mark-Jason Dominus 	  			    mjd@central.cis.upenn.edu