CPS 202
Mount
Final Exam

Short Questions (5 points each, 100 total)

  1. List the three types of loops.
  2. How many bits are in a byte?
  3. How many bytes are in a short int?
  4. What effect does adding the const keyword to a variable declaration have?
  5. Name the two floating-point variable types.
  6. Every C statement must end with what?
  7. What is enclosed by two curly braces?
  8. The * (asterisk) character can be used for two things. What are they?
  9. What does (!0) evaluate to?
  10. What standard C function is used to terminate a program from anywhere in the program?
  11. What function must every C program have?
  12. What are the two boolean values?
  13. Name three things the preprocessor does.
  14. An array name is just like a pointer, but with one key difference - what is it?
  15. List all of the hexadecimal digits.
  16. What is the printf format for printing: An int? A char? A float? A long? An unsigned short?
  17. What is the name of \n? Of \t? Of \a? Of \r? Of \b?
  18. When a function returns nothing, what is its return value in a prototype?
  19. What operation does % perform?
  20. Under what standard did the // comment type become a part of the C language?

Turn each of the following descriptions into C code. For example:
Declare an int variable called "speed": int speed;
(5 points each, 25 total)

  1. Declare a string named "address", for a length of 50.
  2. Prototype a function called "double" that returns a long and takes a long parameter.
  3. Assign the address of an integer called count to an integer pointer called ptr.
  4. Declare a static int variable named "count," initialized to -1.
  5. Declare a function called volume that returns a float and take three floats, h, w, and d, as parameters.

Each of the following individual lines was pulled from within large programs. Each has one or more bugs - fix them. (Note that because you don't have the context in which the line appears, some have more than one possible correct answer) (5 points each, 50 total)

  1. printf('Hello World!');
  2. int #_students;
  3. if (speed =< 65) printf("Speed is OK\n");
  4. 4 = n;
  5. int unsigned age;
  6. #include <stdio.h>;
  7. auto static int area;
  8. unsigned int flag = -1;
  9. &n = 6;
  10. void timesplit(int total_minutes, int &hours, int &minutes);

Long Questions - explain each in a full sentence or two (10 points each, 50 total)

  1. Explain the difference between floating point and integer variables. Include information on floating point and integer math.
  2. What is the importance of indentation? Provide a small bit of C code where the lack of indentation could lead to confusion.
  3. What is the importance of commenting? Who benefits from comments?
  4. What is meant by "operator precedence?" How do you force precedence?
  5. Explain the difference between static and automatic variables. Is there a default? When would you use one over the other?

Short Programming (15 points each, 30 total)

  1. Write this while loop as a for loop:
     x=0;
     while (x < 10) {
       printf("%d times 2 is %d\n",x,x*2);
       x++;
     }
     
  2. The area of a triangle is equal to ab/2, where a is the length of the base, and b is the height. Write a function to return the area of a triangle, given its base and height. Hint: because we're dividing, the function should return a float.

Long Programming 1

You are attempting to catalog your book collection. In your first attempt at it, you decide to write a program to print a catalog sheet. The sheet will print 25 double-spaced rows of blanks to be filled in. To save space, you will print two separate columns of blanks. Your program must use at least one loop. Write the program to make the output look like this: (25 points)

                                   Book Catalog

    Author       Title       ISBN #           Author       Title       ISBN #

 1. ____________ ___________ _________   26.  ____________ ___________ _________

 2. ____________ ___________ _________   27.  ____________ ___________ _________

 3. ____________ ___________ _________   28.  ____________ ___________ _________
. . .

24. ____________ ___________ _________   49.  ____________ ___________ _________

25. ____________ ___________ _________   50.  ____________ ___________ _________

Long Programming 2

Your form works pretty well, but as you capture the data for printing, you find that different printers can fit more rows than others - and when you import the data into a word processing program, the double spacing is more trouble than it is worth. Modify the program to allow a variable number of rows, and to make double spacing optional, taking the data from prompts, using scanf to read the data. Allow a maximum of 60 rows. Here is a sample of a run of the program. (20 points)

c:\> bookform
Enter the number of rows to print: 70
 * Please enter a value from 1 to 60
Enter the number of rows to print: 40
Double space (y/n): U
 * Please enter either Y or N
Double space (y/n): N

                                   Book Catalog

    Author       Title       ISBN #           Author       Title       ISBN #

 1. ____________ ___________ _________   41.  ____________ ___________ _________
 2. ____________ ___________ _________   42.  ____________ ___________ _________
 3. ____________ ___________ _________   43.  ____________ ___________ _________
. . .
39. ____________ ___________ _________   79.  ____________ ___________ _________
40. ____________ ___________ _________   80.  ____________ ___________ _________

Extra Credit 1

To make your program even more usable, you decide to rewrite it to accept the "lines" and "double" values on the command line. Here is a sample of a run of the program. (15 points)

c:\> bookform
usage: bookform lines double
  * please enter a value for lines (1-60)
  * please enter a value for double (Y or N)
c:\> bookform 50
usage: bookform lines double
  * please enter a value for double (Y or N)
c:\> bookform 100 N
usage: bookform lines double
  * rows value must be between 1 and 60
c:\> bookform 40 N

                                   Book Catalog

    Author       Title       ISBN #           Author       Title       ISBN #

 1. ____________ ___________ _________   41.  ____________ ___________ _________
 2. ____________ ___________ _________   42.  ____________ ___________ _________
 3. ____________ ___________ _________   43.  ____________ ___________ _________
. . .
39. ____________ ___________ _________   79.  ____________ ___________ _________
40. ____________ ___________ _________   80.  ____________ ___________ _________

Extra Credit 2

Writing stuff down is fine, but you decide it would be better to store your book collection data on disk. You're not exactly sure how to write database records to disk, so you decide to write a stub for the "write_data" function, but you can write the rest of the program. Make it look something like this (underlines indicate user input): (15 points)

New Book entry...
Enter Author's Name (blank to end): KN King
Enter Title: C Programming
Enter ISBN #: 0-393-96945-2
Saving "C Programming" by KN King to database...

New Book entry...
Enter Author's Name (blank to end):

Back to Main CPS 202 Page

Last Modified: 13 Apr 2002