CMSC 104, Spring 2005

Exam 3 Sample Questions

Our Final Exam will be held on Dec 16th
at 1:00 pm in Room MP 101

if you need help email to: Patti Ordóñez ordopa1@umbc.edu


True/False Questions

Circle TRUE or FALSE for each of the following questions.

  1. TRUE/FALSE
    switch is a legal identifier in C.

  2. TRUE/FALSE
    The statement printf("%5d\n", 3); prints the number 3 five times.

  3. TRUE/FALSE
    The statement printf("%d\n", 3, 5); prints the number 3 five times.

  4. TRUE/FALSE
    The statement printf("%5d\n", 3); prints the number 5 three times.

  5. TRUE/FALSE
    A function prototype tells the compiler the type of the value that is returned by the function.

  6. TRUE/FALSE
    A function prototype tells the compiler nothing about the value that is returned by the function.

  7. TRUE/FALSE
    When a function is called, its local variables initially contain the value 0.

  8. TRUE/FALSE
    If a is a variable of type int, after the statement a = 17.76 has been executed, a has a value of 17.

  9. TRUE/FALSE
    If a is a variable of type int, after the statement a = 17.76 has been executed, a has a value of 18.

  10. TRUE/FALSE
    If a is a variable of type float, after the statement a = 17 has been executed, a has a value of 17 .

  11. TRUE/FALSE
    If a is a variable of type float, after the statement a = 17 has been executed, a has a value of 17.0.

  12. TRUE/FALSE
    A function with return type void always returns the value 0.

  13. TRUE/FALSE
    When a function is called, the number of arguments passed should match the number of formal parameters given in the function's prototype.

  14. TRUE/FALSE
    After a function has returned, the values stored in its local variables are copied into variables in the calling function that have the same name.

  15. TRUE/FALSE
    The values stored in the local variables of a function are not saved when the function exits.

  16. TRUE/FALSE
    The prototype of a function is used mainly as a comment to the reader, but is not useful to the compiler.

  17. TRUE/FALSE
    If the integer variables a and b are holding the values 0 and 1, respectively, what is the value of the following expression : ( a || b ) ?

  18. TRUE/FALSE
    If the integer variables a and b are holding the values 0 and 1, respectively, what is the value of the following expression : ( a && b ) ?

  19. TRUE/FALSE
    If the integer variables a and b are holding the values 1 and 2, respectively, what is the value of the following expression : ( a + b ) ?

  20. TRUE/FALSE
    If the integer variables a and b are holding the values 0 and 6, respectively, after the statement a += b++ the new values of a and b are both 7.

  21. TRUE/FALSE
    If the integer variables a and b are holding the values 0 and 6, respectively, after the statement a += ++b the new values of a and b are both 7.

  22. TRUE/FALSE
    If the integer variables a and b are holding the values 0 and 6, respectively, after the statement a += b++ the new values of a and b are 6 and 7, respectively

  23. TRUE/FALSE
    When an array is passed to a function, only the address of the array is passed to the function.

  24. TRUE/FALSE
    When an array is passed to a function, a copy of the elements is made and passed to the function to manipulate.

  25. TRUE/FALSE
    When an array is passed to a function, the address of the array is passed to the function and a copy of the elements is made for the function to manipulate.

  26. TRUE/FALSE
    The preprocessor directive #define RING 1 tells the C preprocessor to replace each occurrence of the constant RING with 1.

  27. TRUE/FALSE
    The default case is required in the switch selection structure.

  28. TRUE/FALSE
    The default case must be the last case in the switch selection structure.

  29. TRUE/FALSE
    The break statement is required in the default case of the switch selection structure.

  30. TRUE/ FALSE
    An array can store many types of values simultaneously.

  31. TRUE/ FALSE
    Array subscripts must always start at 0.

  32. TRUE/ FALSE
    The fourth element of an array named bob is bob[4]

  33. TRUE/ FALSE
    All arrays with 10 elements require the same amount of memory.


Multiple Choice Questions

  1. What is the output of the following code fragment? int a = 3, b = 17 ; a = b % a ; b = ++a + 5 ; printf("a = %d, b = %d\n", a, b) ;

      [a.] a = 3, b = 8
      [b.] a = 2, b = 7
      [c.] a = 3, b = 7
      [d.] a = 2, b = 8
      [e.] None of the above.

  2. For the following code fragment, TRUE has been #defined to be 1, and FALSE has been #defined to be 0. Note that there is an assignment operator in the condition of the first if statement. The output of the following program fragment is:


    test = FALSE ;
    if (test = TRUE) {
        printf("Beam down the landing party.\n") ;
    } else if (test == FALSE) { \
        printf("Jim, he's dead.\n") ;
    } else if (test == TRUE) {
        printf("Scotty, four to beam up.\n") ;
    }

      [a.] Beam down the landing party.
      [b.] Jim, he's dead.
      [c.] Scotty, four to beam up.
      [d.] No output is produced

  3. Which of the following is the function prototype for a function AlphaBeta that takes two double parameters and does not return any values.

      [a.] AlphaBeta(double, double);
      [b.] void AlphaBeta(double, double);
      [c.] void AlphaBeta(double x, y);
      [d.] double AlphaBeta(double, double);

  4. Which of the following is the function prototype for a function Bob that takes one double parameter and returns an integer value.

      [a.] void Bob (int, double);
      [b.] void Bob (double, int);
      [c.] int Bob (double);
      [d.] int Bob (int, double);

  5. Which of the following is the function prototype for a function Bill that takes two integer parameters and returns a double value

      [a.] Bill (int, int);
      [b.] double Bill (int, int);
      [c.] void Bill (double, int, int);
      [d.] double Bill (int * 2);

  6. When an array parameter is passed to a function

      [a.] the elements of the actual array parameter are copied into the elements of the formal array parameter.
      [b.] the elements of the formal array parameter are copied into the elements of the actual array parameter.
      [c.] the formal parameter is a pointer that holds the address of the actual array parameter.
      [d.] the programmer must write code which allocates enough space for the function to store the array.

  7. Which of the following statements will print out the float variable x with exactly 1 decimal place, e.g. 123.4

      [a.] printf ("%.1f\n", x);
      [b.] printf ("%1f\n", x);
      [c.] printf ("%.1e\n", x);
      [d.] printf ("%1e\n", x);

  8. A single C statement that assigns the sum of x and y to z and then increments the value of x by 1 after the calculation.

      [a.] z = y + (x + 1);
      [b.] z = ++x + y;
      [c.] z = y + x++;
      [d.] none of the above

  9. A single C statement that decrements the variable x by one then subtracts it from the variable total is

      [a.] total = total - x--;
      [b.] total -= --x;
      [c.] total -= (x - 1);
      [d.] none of the above

  10. A single C statement that adds the variable x to the variable total and then decrements x by one is

      [a.] total = total + x--;
      [b.] total += --x;
      [c.] total += (x - 1);
      [d.] none of the above

  11. Assuming that the variables x and product each have the value 5, then the new values of x and product after the statement product *= x++; are

      [a.] product = 25 and x = 5
      [b.] product = 25 and x = 6
      [c.] product = 30 and x = 5
      [d.] product = 30 and x = 6

  12. Assuming that the variables high and low contain the values 3 and 5, respectively, then the new values of high and low after the statement high *= --low + 4 are

      [a.] high = 24 and low = 4
      [b.] high = 24 and low = 8
      [c.] high = 19 and low = 4
      [d.] high = 19 and low = 8

  13. Assuming that the variables a and b contain the values 20 and 3, respectively, then the new values of a and b after the statement a -= --b * 2 are

      [a.] a = 14 and b = 2
      [b.] a = 16 and b = 2
      [c.] a = 14 and b = 4
      [d.] a = 16 and b = 4


Short Answers

In the following questions, no syntax errors have been put in deliberately. (So, "there is a syntax error" is NOT the right answer and will receive no credit.) Please mark your final answer clearly. In order to receive partial credit, you must show your work.

  1. Please briefly describe the function of each of the following: the preprocessor, the compiler and the linker.

  2. Write a function called SumArray that returns the sum of the first n elements of an array of floats, where the array and n are passed to it.

  3. Give a function prototype for a function called Hypotenuse that takes two double precision floating point arguments, side1 and side2 and returns a double precision floating point result.

  4. Give a function prototype for a function called Smallest that takes three integer arguments, x, y and z and returns an integer.

  5. Give a function prototype for a function called Instructions that does not have any arguments and does not return a value.

  6. What happens if your program tries to access an array element with an out-of-range subscript?

  7. How are the elements of an array stored in memory ?

  8. What is the output of the following program? Don't wory about the exact number of spaces to appear before or after any numeric output. If the program goes into an infinite loop, you need show only the first five lines of output. Assume that the program as shown compiles correctly. #include <stdio.h> </stdio.h> int Deep(int, int) ; int Space(int) ; int main() { int a = 2, b = 3, c = 3 ; a = Deep (b + 5, c) ; b = Space (c) ; printf ("main: a = %d, b = %d, c= %d\n", a, b , c) ; return 0; } int Deep (int x, int y) { int z ; z = x - y ; printf ("Deep: x = %d, y = %d, z = %d\n", x, y, z) ; return (z) ; } int Space (int c) { int a, b ; a = 1 ; b = c * 2 + a ; a = b + 5 ; c = Deep (a, b) ; printf ("space: a = %d, b = %d, c = %d\n", a, b, c) ; return (b) ; }

  9. What is the difference between pass by value and pass by reference?

  10. Which of C's data types can be used in an array?

  11. In a one-dimensional array declared with n elements, what is the index of the last element ?

  12. Write the statements that would declare a 10-element integer array and initialize all of its elements to 1.

  13. Write a function called Average that takes three integer parameters n1, n2, and n3 and returns the value of the average of the three as a double.

  14. Write a procedure called PrintBox that will print a rectangle made up of any character passed to it. Here is the prototype: void PrintBox (int rows, int columns, char ch); This call to PrintBox, PrintBox(3, 10, '$');
    should print: $$$$$$$$$$ $$$$$$$$$$ $$$$$$$$$$

  15. Write a function called NumDigits that has a single formal parameter of type int and returns the number of digits in that integer.

  16. Fill in the blanks below. The function FindNet computes a worker's weekly net pay given hours (an integer), rate (a double), and exempt (a boolean) as input parameters. The net pay for the worker should be returned as a double. If the worker turns in more than forty hours and the value of exempt is FALSE, then pay 1 1/2 times the normal rate ("time and a half") for any hours worked over forty. If gross pay exceeds $800, subtract 33% for taxes. Otherwise, subtract 25% for taxes. Include a statement to print the worker's hours and net pay for the week. (The net pay will be in dollars and cents, so choose the output format accordingly. The number of underscore characters shown is not necessarily an indication of the length of the correct response.) ________ FindNet ( int hours, ________ ______ , int _______ ) { double ________ , __________ ; int ___________ ; if ( _______ > 40 _____ (!exempt ) ) { extraHours = hours - 40; grossPay = (40 * _______ ) + extraHours * (rate * _____); } else { grossPay = hours * _______ ; } if (grossPay > ________ ) { netPay = grossPay - grossPay * _______ ; } ______ { netPay = grossPay - grossPay * _______ ; } ________ ("Hours = %d and Net Pay = _______ \n", hours, netPay); ________ ( __________ ); }

  17. Write a set of one or more if statements that will print out one of the brief messages given in the table below, depending on a person's grade point average. Assume that the grade point average has already been read into a variable called gpa. Include code to print a warning message if the value of gpa is not in the range 0.0 to 4.0.

    gpa rangestudent rating
    3.50 - 4.0 Dean's list
    2.0 - 3.49 Satisfactory
    1.0 - 1.99 Probation
    0.0 - 0.99 Suspended


    Determine the output generated by the following code segments, assuming the surrounding program code (whatever it is) compiles correctly.


  18. printf ("%.2f", 3.859143);


  19. printf ("%.2f", 1.3e2);


  20. int a = 2, b = 3; b *= a; a += b++; printf ("%d %d\n", a, b);


  21. void F (int x); void G (int x); int main () { G (3); F (2); G (5); return 0; } void F (int x) { printf ("%d ", x); } void G (int x) { F (x + 1); printf ("%d ", x); }


  22. int X (int m); int Y (int n); int main () { int r, s; r = X (5); s = Y (10); printf ("main: %d %d\n"), r, s); return 0; } int X (int m) { printf ("X: %d\n", m); return (m + 2); } int Y (int n) { printf ("Y: %d %d\n", n, X (n)); return (n * 3); }


  23. int a = 5, b = 4; b += a; a -= ++b; printf ("%d %d\n", a, b); a = 13; b = 4; printf ("%d %d\n", a / b, b / a); printf ("%d %d\n", a % b, b % a);


  24. int n = 1234; while (n > 0) { n /= 10; printf ("%d\n", n); }


  25. int m; for (m = 1; m <= 5; m++) { switch (m) { case 1: printf ("one"); break; case 2: printf ("two"); break; case 3: printf ("three"); break; default: printf ("Default case"); } printf ("\n"); }


  26. int F1 (int a, int b); int F2 (int b, int c); int main() { int a = 1, b = 2, c = 3; c = F1 (a, b); printf ("main: %d %d %d\n", a, b, c); b = F2 (a, c); printf ("main: %d %d %d\n", a, b, c); return 0; } int F1 (int a, int b) { printf ("F1: %d %d\n", a, b); return a; } int F2 (int b, int c) { printf ("F2: %d %d\n", b, c); return c; }


  27. int F1 (int a, int b); int F2 (int a, int b); int main() { int a = 1, b = 2, c = 3; c = F1 (a, b); printf ("%d %d %d\n", a, b, c); b = F2 (a, c); printf ("%d %d %d\n", a, b, c); a = F2 ( F1 (a, c), b); printf ("%d %d %d\n", a, b, c); b = F2 (a, c) / F1 (b, c); printf ("%d %d %d\n", a, b, F1 (a, b) ); return 0; } int F1 (int a, int b) { return (a + b + 3); } int F2 (int a, int b) { return ( (a + b) % 3 ); }