***PRACTICE EXAM 01 for***
CS16 Midterm Exam 1
E01, 09S, Phill Conrad, UC Santa Barbara
Actual exam scheduled for: 10/21/2009

Name: ________________________________________________________


Umail Address: __________________________________@ umail.ucsb.edu


Circle Lab section:        8AM             10AM           11AM             noon

Link to Printer Friendly PDF Version


Please write your name only on this page. That allows me to grade your exams without knowing whose exam I am grading.

This exam is closed book, closed notes, closed mouth, cell phone off,
except for:

There are 100 points worth of questions on the exam, and you have 50 minutes to complete the exam.

A hint for allocating your time:


  1. (20 pts) Write the definition of a C function that takes two numbers (real numbers, not necessarily integers) as parameters, and returns the larger of the two numbers (or if they are equal, the value they are both equal to.)

    Write ONLY the function definition—for this question, I do NOT want a complete C program, so do NOT include any extraneous stuff such as #include <stdio.h> or a main function. (See solution)






  2. (20 pts) There is an incomplete program below. It includes:

    Add the code that would be necessary, so that this is a complete interactive program that will convert from inches to centimeters. Include code in main that prompts the user for input (using printf/scanf) and then prints the result of a function call to inToCm() for the user to see the result.

    See the extra handout for sample output from this program.

    Syntax hints:

    // inToCmInteractive.c     Answer to a practice exam quesiton
    // P. Conrad, for CS16   10/16/2009










    int main() {


















    }
    double inToCm(int inches) { return inches * 2.54; }
  3. (20 pts) For each of the following C expressions, fill in the value, and then circle the type. The first two are done for you as an example.
    (See solution)

    expression
    value
    type
    3
         
         3   
           
       int       double        char         char *      
    0.5 * 0.5

    0.25

       int       double        char         char *      
    '3'



       int       double         char         char *      
    "3"



       int       double         char         char *      
    1 / 10

                               

       int       double         char         char *      
    5 / 2.0

                               

       int       double         char         char *      
    26 % 5

                               

       int       double         char         char *      
    5 % 2

                               

       int       double         char         char *      
    1.0 / 10

                               

       int       double         char         char *      
    1 + 3 * 5

                               

       int       double         char         char *      
    1 + 1 / 2

                               

       int       double         char         char *      
     
     2.0 / 4.0 + 1 

                               

       int       double         char         char *      





  4. (12 pts) Convert each of the following numbers according to the instructions given:
    (No solution given—you can practice these types of questions here)

    1. Convert 110 111 010 from base 2 to octal




    2. Convert 1110 0111 from base 2 to base 10




    3. Convert 0011 1101 0011 1000 from base 2 to base 16




    4. Convert 66 from base 10 to base 2




    5. Convert 157a from hexadecimal to binary




    6. Convert 644 from octal to binary


  5. (8 pts) In test-driven development, we often write a stub before we write the main function. Why?

    (no solution given—but if you review what we did in lab02, the answer will reveal itself to you.)

































End of Exam

Total points: ?

***PRACTICE EXAM 01 for***
CS16 Midterm Exam 1

Extra Handout

 

Sample Output from program in question 2:


-bash-3.2$ ./inToCmInteractive 
This prorgram converts inches to cm
Please enter inches: 2.54
The answer is: 5.080000
-bash-3.2$ ./inToCmInteractive
This prorgram converts inches to cm
Please enter inches: 1
The answer is: 2.540000
-bash-3.2$ ./inToCmInteractive
This prorgram converts inches to cm
Please enter inches: 2
The answer is: 5.080000
-bash-3.2$ ./inToCmInteractive
This prorgram converts inches to cm
Please enter inches: 36
The answer is: 91.440000
-bash-3.2$