CSCI 116 Fall 2004 Exam #1

1 October 2004

 

Instructions:  Write your answers in the space provided.  If you need more room, use the back of one of the other sheets of the test, and indicate where I can find your work.  For partial credit, be sure to show your work!  This test is closed book, closed notes, closed neighbor, open mind.  Any calculations are simple enough that you don’t need to use a calculator.  Read through the entire test before starting to answer any of the questions so that you can answer the easiest questions first and maximize your score.  Not counting the bonus question, there are 100 points on this test.  You have 50 minutes to complete the test.  Good luck, and have fun!

 

1.      (30 points)  What are the values of the following expressions?

a.      3 + 2

 

 

 

 

 

b.      5 - 8 - 3

 

 

 

 

 

c.      14.5 / 5

 

 

 

 

 

d.      3+4  *  7-5

 

 

 

 

 

e.      1 == 0

 

 

 

 

 

f.        (3 == 4) || (7 > 5)

 

 

 

 

g.      3 / 4 * 8

 

 

 

 

 

h.      ! ( (3 != 4) && (6 <= 8) )

 

 

 

 

 

i.         (2 * (3 + (11 – 5) / 2))

 

 

 

 

 

j.         11 % 4

 

 

 

 

 

2.      (30 points)  Write C++ code to do the requested operations.  You do not need to write anything except for what is specified!

a.      Declare integer variables named foo and bar.

 

 

 

 

 

 

 

 

b.      Specify the prototype for a function named red that takes an integer and a character as parameters (in that order, passed by value), and returns a double precision floating point number.

 

 

 

 

 

 

 

 

 

 

c.      Write an if statement that tests the value of the variable reader.  If the value is positive, set the variable writer to -1; if the value is negative, set the value of writer to 1; if the value is 0, set the value of writer to 0.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

d.      Write a while loop that adds up 20 even numbers, starting with 2 and stores the result in the variable sum.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

e.      Write code to ask the user to input an integer and then to read and store the number in the variable input.

 

 

 

 

 

 

 

 

 

f.        Write code to add the remainder of count after division by 7 to the value stored in number, and to store the result in answer.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3.      (10 points)  Answer the following questions in the space provided:

a.      Describe one difference between a do while loop and a while loop, and explain when you would choose to use each.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

b.      What is the difference between pass by value and pass by reference?  Describe an instance where it matters which we use.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4.      (15 points)  Write a C++ function that takes two integers as parameters, and returns the sum of the two as a double.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5.      (15 points)  Write a complete program that prints out the message, “You said we wouldn’t have to write a complete program!” on a separate line, and then exits.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Bonus:  On a train, Smithe, Robins, and John are the fireman, conductor, and the engineer, but NOT respectively. Also aboard the train are three businessmen who have the same names: a Mr. Smithe, a Mr. Robins, and a Mr. John.

Mr. Robins Lives in New York. The conductor lives exactly halfway between Columbus and New York. Mr. John earns exactly $20,000 per year. The conductor's nearest neighbor, one of the passengers, earns exactly three times as much as the conductor. Smithe beats the fireman in billiards. The passenger whose name is the same as the conductor's lives in Columbus.

 

Who is the Engineer?