Computer Science 157 – Fall 1999

11/08/99                 Mid Term Test # 2                        Test Form A

 

Name:

 

Instructions:

                Answer ALL questions on the answer sheet provided.

                Hand in TEST, Answer Sheet, and Help Sheet, each with your name on it.

                Remember to put the letter of your test form on your answer sheet.

                Please ask any clarifying questions you have (come up).

 

Completion (3 points each)

 

1.        In C, a ________ gives advance notice of the interface to a function.  This is necessary when a function is defined after the code that calls the function, so that the compiler can check the call for validity. This is also used inside a .h file. It is essentially an advance description of the functions interface.

 

2.        A function that calls itself is a(n) _______ function.

 

3. The number used to refer to a particular element of an array is called its ________. This “position number” is contained within square brackets.

 

 

Short Answer (points as shown)

 

 (4 points)

4. List all actual parameters associated with the Junk function below.

     

int Junk (int a, int b, double c) {

  int Temp;

  Temp = a;

  a = b;

  b = Temp

  return b;

 }

int main () {

  int x = 5;

  int y = 6;

  int z = 7;

  int r = 3;

  int s = 1;

  int t = 9;

  double u = 5.5;

  double m = 5.5;

  z = Junk(x,y,m);

  t = Junk(r,s,u);

 

 

Valid/Invalid (4 points each)

For each, tell whether valid or invalid. If invalid, tell why!

 

5. int junk[10];

junk[10] = 3;

 

6. double junk[10];

junk[2.5] = 3;

 

 

 

7.

double y = 2.5;

  switch (y) {

    case 1: case 2:

      printf( "1" );

      break;

    case 3: case 4:

      printf( "2" );

      break;

    default:

      printf( "3" );

    }

 

For questions 8-9, using the following function heading:

           int Jeez2 (int a, int b, double c) { ...

and the following constant and variable declarations in the main program:

                int num1 = 0;

                int num2 = 1;

                int num3 = 2;

                int num4 = 3;

                double val1 = 0.1;

                double val2 = 1.1;

which function calls are valid?  If the function call is invalid, explain why.

 

8. num1 = Jeez2(num1, 4, val1);

 

9. num4 = Jeez2(num1 + num2,  num3,  val2);

 

// function refinition

10. double xxx (int, int) {

          … // valid from here on out

 

 

Doing and Understanding: (points as shown)

 

(10 points)

11. What is the output of the following program?

  

int OneThing(int x) {

   printf("X: %2d \n”,x);

   x += 3;

   return x;

 }

int TestIt(int z) {

   int q;

   printf("Z: %2d \n”,z);

   z++;

   q = OneThing (z);

   printf("Z: %2d  Q: %2d \n”,z,q);

   return q;

 }

int main() {

   int d = 0;

   int e;

   printf("D: %2d  \n”,d);

   e = TestIt(d);

   printf("D: %2d  E: %2d \n",d,e);

   return 0;

 }

 

 

 (4 points)

12. What is displayed by the following code fragment:

 

  int i = 5;

  switch (i) {

    case 1: case 2: case 3: case 6:

      printf( "1" );

      break;

    case 4: case 5: case 7:

      printf( "2" );

    case 8:

      printf( "3" );

      break;

    default:

      printf( "4" );

    }

 

 

 (5 points)

13. What is the output of the following code?

 

int Junk2 (int a, int b) {

    printf("A”);

    return (a - b);

  }

 

int Junk1 (int a) {

    printf ("B”);

    return (a + a);

  }

 

int main() {

   int y = 9;

   int z = 5;

   int x = Junk2(y,z);

   printf (“%d “,x );

   int x = Junk1(z);

   printf (“%d “,x );

 }

 

 

(6 points)

14. What is the output of the following program?

 

int main() (

                int data[7] = { 4, 5, 6, 7 };

                int cnt;

                for (cnt = 0; cnt < 7; cnt++) {

                                data[cnt] += cnt;

                }

                for (cnt = 0; cnt < 7; cnt++) {

                                printf(“%d \n”,data[cnt]);

                }

 }

 

 

 

(8 points)

15. int doSwap (int x, int y) {

                int z = x + y;

                x = x + z;

                y = y + x;

                printf (“ %d %d %d \n”,x,y,z);

                return z;

  }

int main() {

                int a = 3;

                int b = 5;

                int c = 9;

                c = doSwap(a,b);

                printf (“ %d %d %d \n”,a,b,c);

                return 0;

}

 

 

(8 points)

16. What is the output of the following program segment?

 

                int cnt;

                int x;

                int y;

                int junk[15] = {10, 12, 14, 1, 3, 5, 7, 9};

                x = junk[4];

                y = junk[7];

                junk[4] = junk[6];

                junk[7] = junk[2];

                junk[1] = x;

                junk[3] = y;

                for (cnt = 0; cnt < 8; cnt++)

                                printf(“  %d  “);

                }

 

 

Writing Code (points as shown)

 

 (10 points)

17. Write a sequence of C statements that go through the 20 elements of the integer array called data, and finds out how many of the values are larger than 100.

 

(12 points)

18.  A parking garage charges a $2.00 minimum fee to park for up to 3 hours. The garage charges an additional $0.50 per hour for each hour in excess of 3 hours. Write a function that, given a (whole ) number of hours parked, returns the total charge. Also write a call to the function from main, where a variable named currHours holds the number of hours parked by the current customer.