CS104 Final Exam Master
Spring 2002


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
    int is a keyword in C.

  3. TRUE/FALSE
    main is a legal identifier in C.

  4. TRUE/FALSE
    If n is an integer variable, the value of the expression n % 5 is 0 whenever n is a multiple of 5.

  5. TRUE/FALSE
    If n is an integer variable, the value of the expression n % 7 is 0 whenever n is a not a multiple of 7.

  6. TRUE/FALSE
    If n is an integer variable, the value of the expression n % 6 is always less than 6.

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

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

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

  10. TRUE/FALSE
    A function prototype gives the compiler the value returned by the function.

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

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

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

  14. TRUE/FALSE
    If a is a variable of type int, the statement a = 17.76 is an expression with type int and value 17.

  15. TRUE/FALSE
    If a is a variable of type int, the statement a = 17.76 is an expression with type double and value 17.76.

  16. TRUE/FALSE
    If a is a variable of type int, the statement a = 17.76 is an expression with type double and value 17.

  17. TRUE/FALSE
    In the C programming language, variable names can have at most 8 characters.

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

  19. 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.

  20. TRUE/FALSE
    After a function has returned, the values stored in its local variables are copied into variables in the main function with the same name.

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

  22. TRUE/FALSE
    The preprocessor directive #include instructs the preprocessor to place the contents of the file foo.h into the code at that point.

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

  24. TRUE/FALSE
    In UNIX, the file foo.o usually contains the the machine language translation of the source code file foo.c .

  25. TRUE/FALSE
    The #include preprocessor directive is used to insert one file into another.

  26. TRUE/FALSE
    What is the value of the C expression 2 * 6 - 5 + 8 % 5

  27. TRUE/FALSE
    What is the value of the C expression 3 * 5 - 19 + 7 % 4

  28. TRUE/FALSE
    What is the value of the C expression 5 * 5 - 19 - 6 % 7

  29. 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 ) ?

  30. 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 ) ?

  31. 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 ) ?

  32. TRUE/FALSE
    What is the value of the C expression 3 * 5 - 12 ?

  33. 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.

  34. 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.

  35. 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

  36. TRUE/FALSE
    If the integer variables a and b are holding the values 5 and 2, respectively, the expression
    a / b produces the result 2

  37. TRUE/FALSE
    If the integer variables a and b are holding the values 5 and 2, respectively, the expression
    a / b produces the result 2.5

  38. TRUE/FALSE
    If the integer variables a and b are holding the values 5 and 2, respectively, and c is a double, after executing the following statement, the value of c is 2.5
    c = a / b ;

  39. TRUE/FALSE
    A procedure returns a 0 or a 1.

  40. TRUE/FALSE
    A procedure has no return value.

  41. TRUE/FALSE
    A predicate function returns a 0 or a 1.

  42. TRUE/FALSE
    Any code that can be written as a for loop can also be written as a while loop.

  43. TRUE/FALSE
    A call to the srand() function returns a random integer.

  44. TRUE/FALSE
    A call to the rand() function returns a random integer.

  45. TRUE/FALSE
    A call to the srand() function initializes the pseudo-random number generator.

  46. TRUE/FALSE
    The value of the expression rand() % 5 is an integer between 1 and 5 inclusive.

  47. TRUE/FALSE
    The value of the expression rand() % 7 + 1 is an integer between 1 and 7, inclusive.

  48. TRUE/FALSE
    The value of the expression rand() % 5 is an integer between 0 and 4, inclusive.

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

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

  51. 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.

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

  53. TRUE/FALSE
    All variables must be declared and given a type before they are used

  54. TRUE/FALSE
    Variable declarations can appear anywhere in the body of a function.

  55. TRUE/FALSE
    Each call to printf causes printing to begin at the next line.

  56. TRUE/FALSE
    You must use the newline ('\n') character to start printing at the beginning of the next line.

  57. TRUE/FALSE
    A C program that prints three lines of output must contain three printf statements.

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

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

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

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

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

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

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


Multiple Choice Questions

  1. What is the value of the expression 12 - 10 - 8 * 6 % 4 + 2 % 1

  2. The expression 18 + 2.5

  3. 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) ;

  4. Execution of a break statement in the body of a while loop

  5. Execution of a continue statement in the body of a for loop

  6. Determine the output of the code fragment. int a = 5, b = 4, c = 1 ; if ( a < b + 1 ) { printf("Tea, Earl Grey, Hot!\n") ; } else if ( a > b + c ) { printf("Ahead warp factor 9. Engage!\n") ; } else if ( a % b >= c ) { printf("Warp core breach in 20 seconds!\n") ; } else { printf("I sense a lot of anxiety in this room.\n") ; }

  7. What is the effect of the following code? int i, total ; for (i = 1 ; i <= 15 ; i++) { if ( i % 3 == 0) { printf("%d ", i) ; } } printf("\n\n");

  8. 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 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") ; }

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

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

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

  12. When an array parameter is passed to a function

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

  14. 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.

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

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

  17. 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

  18. 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

  19. 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


Short Answers

In the following questions, no syntax errors have 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. 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.

  2. Write a C code fragment using a for loop that prints the numbers 1 to 10 side-by-side on the same line with 3 spaces between each number

  3. Write a C code fragment using a while loop that prints the even numbers between 1 and 10 (inclusive)side-by-side on the same line with 3 spaces between each number

  4. Write a C code fragment using a for loop that prints a 7 x 7 square out of asterisks.

  5. Write a C code fragment using a for loop that prints the following sequence of numbers: 3, 8, 13, 18, 23

  6. Write a C code fragment using a for loop that calculates and and prints the sum of the even integers from 2 to 30, inclusive.

  7. Write a C code fragment using a for loop that calculates and prints the product of the odd integers from 1 to 15, inclusive.

  8. 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.

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

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

  11. Write a single C statement that will print a random number from the set 2, 4, 6, 8, 10

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

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

  14. All programs can be written in terms of three control structures: _____________________, ______________________ and ___________________.

  15. Rewrite the following code segment that uses a for loop as an equivalent code segment that uses a while loop. for (i = LOWER; i <= UPPER; i++) { if (i % 5 == 0) { printf("%d is a multiple of 5\n", i); } else { printf("%d is not\n", i); } }

  16. 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> int Deep(int, int) ; int Space(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) ; } 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) ; }
  17. Given the following declarations and initializations: int m = 5, n = 8; float x = 9.0; What is the value and type of the expression m + n * x ?

  18. This question tests your knowledge of nested for loops. Please put carriage returns in the proper places. What is the output of the following code ? int i, j; for (i = 0; i <= 3; i++) { for (j = 0; j <= 4; j++) { if ( (i + 1 == j) || (j + i == 4) ) { printf ("+"); } else { printf ("o"); } } printf ("\n"); }
  19. In the nested for loop code above, how many times is the if statement executed?

  20. Write a simple loop which prints out the following sequence of numbers: 18 23 28 33 38 43 48

  21. What's the difference between a function prototype and a function header ?

  22. Write a program that inputs 2 floating point values from the keyboard and displays their product.

  23. Write a program that gets 10 integer values that are input from the keyboard, then displays their sum.

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

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

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

  27. 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.

  28. 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: $$$$$$$$$$ $$$$$$$$$$ $$$$$$$$$$

  29. Write a function called Harmonic that takes an integer parameter n, and returns the following sum: 1/1 + 1/2 + 1/3 + . . . + 1/n

    This is called a ``partial Harmonic series''. The various fractions, and the returned value should be of type double.

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

  31. Write a predicate function called IsEven that takes an integer as its single argument.

  32. 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); ________ ( __________ ); }
  33. Fill in the blanks below. The function PrintRandomPhoneNumber generates and prints a random seven-digit phone number that adheres to the rules for phone numbers in the United States. Those rules are: The function takes the seed for the pseudo-random number generator as its single parameter of type int. Choose the output format to guarantee proper printing of the phone number. (The number of underscore characters shown is not necessarily an indication of the length of the correct response.) ________ PrintRandomPhoneNumber ( ________ seed ) { int digit1 , ________, digit3, firstThree, lastFour; /* Seed the random number generator */ _____________(seed); /* Generate the first two digits individually */ digit1 = rand( ) ______ 8 + 2; ________ = rand ( ) % ________ + ________ ; /* Generate the third digit */ ________ = rand ( ) % ________ ; /* Calculate the 3-digit integer called firstThree */ firstThree = digit1 * ________ + ________ * _______ + digit3; /* Generate a four-digit integer */ lastFour = ________( ) % ________ ; /* Print the random phone number */ _______ ("_______-_______\n", _______, ______ ); }
  34. Write a function called RollDice that returns the results of rolling two six-sided dice. You may assume that the pseudo-random number generator has already been seeded in main() or some other function before RollDice is called.

  35. 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

  36. In the list below, circle the items that are NOT legal identfiers in C. 3d XyYzZx ABC123 __yes star*it m me_to-2 main money$

  37. Determine the output of the following program segment: int a = 17, b; b = a; printf ("b = %d\n", b); b = b + 1; printf ("b = %d\n", b); b = a / b; printf ("b = %d\n\n\n", b);
  38. Rewrite the following program, correcting all errors: include (stdio.h) main{} / Program execution begins here / ( people, legs integer; print ("How many people are there?/n); scanf ("%d", people); legs = people * 2 print ("There are %f legs."); [end main]
  39. Given:
    int a = 5, b = 7, c = 15;

    Evaluate each expression below as TRUE or FALSE.

    _______ (1) c / b == 2 _______ (2) c % b <= a % b _______ (3) b + c / a != c - a _______ (4) (b < c) && (c == 7) _______ (5) (c + 1 - b == 0) || (a = 5)


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


  40. printf ("We sell "); printf ("C shells\n"); printf ("by the\nsea shore\n");


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


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


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


  44. int m = 5, n = 8; printf ("%d %d\n", m % n, n % m);


  45. int x = 12, y = 28; printf ("%d %d\n", x / y, y / x);


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


  47. int X (int m); int Y (int n); main () { int r, s; r = X (5); s = Y (10); printf ("main: %d %d\n"), r, s); } 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); }


  48. 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);


  49. int x = 1; if (x > 3) { if (x > 4) { printf ("A"); } else { printf ("B"); } } else if (x < 2) { if (x != 0) { printf ("C"); } } printf ("D");


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


  51. int j, k; for (j = 1; j <= 4; j++) { for (k = 1; k <= j; k++) { printf ("*"); } printf ("\n"); }


  52. int g, h; for (g = 1; g <= 3; g++) { for (h = 0; h < 3; h++) { if (g == h) { printf ("O"); } else { printf ("X"); } } printf ("\n"); }


  53. 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"); }


  54. int F1 (int a, int b); int F2 (int b, int c); 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); } 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; }


  55. int F1 (int a, int b); int F2 (int a, int b); 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) ); } int F1 (int a, int b) { return (a + b + 3); } int F2 (int a, int b) { return ( (a + b) % 3 ); }