CS104 Final Exam Master
Fall 2001

True/False Questions

Circle TRUE or FALSE for each of the following questions.
  1. TRUE/FALSE

  2. switch is a legal identifier in C.
  3. TRUE/FALSE

  4. int is a keyword in C.
  5. TRUE/FALSE

  6. main is a legal identifier in C.
  7. TRUE/FALSE

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  58. 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 ) ?
  59. TRUE/FALSE

  60. 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 ) ?
  61. TRUE/FALSE

  62. 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 ) ?
  63. TRUE/FALSE

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

  66. 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.
  67. TRUE/FALSE

  68. 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.
  69. TRUE/FALSE

  70. 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
  71. TRUE/FALSE

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

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

  76. 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 ;
  77. TRUE/FALSE

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

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

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

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

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

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

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

  92. 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.
  93. TRUE/FALSE

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

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

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

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

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

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

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

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

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

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

  114. Array subscripts must always start at 0.
  115. TRUE/ FALSE

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

  118. 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?
  4.         int a = 3, b = 17 ;
            
            a = b % a ;
            b = ++a + 5 ; 
            printf("a = %d, b = %d\n", a, b) ;
    
    
  5. Execution of a break statement in the body of a while loop
  6. Execution of a continue statement in the body of a for loop
  7. Determine the output of the code fragment.
  8.         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") ;
            }
    
    
  9. What is the effect of the following code?
  10.         int i, total ;
      
            for (i = 1 ; i <= 15 ; i++) 
            {
               if ( i % 3 == 0) 
               {
                  printf("%d  ", i) ;
              }
            }
            printf("\n\n");
    
    
  11. For the following code fragment, TRUE has been #defined to be 1,

  12. 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") ;
            }
    
    
  13. Which of the following is the function prototype for a function AlphaBeta that takes two double parameters and does not return any values.
  14. Which of the following is the function prototype for a function Bob that takes one double parameter and returns an integer value.
  15. Which of the following is the function prototype for a function Bill that takes two integer parameters and returns a double value
  16. When an array parameter is passed to a function
  17. Which of the following statements will print out the float variable x with exactly 1 decimal place, e.g. 123.4
  18. 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.
  19. A single C statement that decrements the variable x by one then subtracts it from the variable total is
  20. A single C statement that adds the variable x to the variable total and then decrements x by one is
  21. 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
  22. 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
  23. 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.
  16.     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);
            }
        }
    
    
  17. 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.
  18. #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) ;
    }
    
    
  19. Given the following declarations and initializations:
  20.     int m = 5, n = 8;
        float x = 9.0;
    
    What is the value and type of the expression m + n * x ?
  21. 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 ?
  22.     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");
        }
    
    
  23. In the nested for loop code above, how many times is the if statement executed?
  24. Write a simple loop which prints out the following sequence of numbers: 18 23 28 33 38 43 48
  25. What's the difference between a function prototype and a function header ?
  26. Write a program that inputs 2 floating point values from the keyboard and displays their product.
  27. Write a program that gets 10 integer values that are input from the keyboard, then displays their sum.
  28. Which of C's data types can be used in an array.
  29. In a one-dimensional array declared with n elements, what is the index of the last element ?
  30. Write the statements that would declare a 10-element integer array and initialize all of its elements to 1.
  31. 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.
  32. Write a procedure called PrintBox that will print a rectangle made up of any character passed to it. Here is the prototype:
  33. void PrintBox (int rows, int columns, char ch);
    
    This call to PrintBox,        PrintBox(3, 10, '$');
     
    should print:
             $$$$$$$$$$
             $$$$$$$$$$
             $$$$$$$$$$
    
    
  34. Write a function called Harmonic that takes an integer parameter n, and returns the following sum:
  35. 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.
  36. Write a function called NumDigits that has a single formal parameter of type int and returns the number of digits in that integer.
  37. 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.)
  38. ________  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);
    
       ________ ( __________ );
    }
    
    
    
  39. 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:
  40. 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", _______, ______ );
    
    }
    
    
    
  41. 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.
  42. 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.
  43. gpa range student rating
    3.50 - 4.0  Dean's list 
    2.0 - 3.49  Satisfactory 
    1.0 - 1.99  Probation 
    0.0 - 0.99  Suspended 
  44. In the list below, circle the items that are NOT legal identfiers in C.
  45.         3d               XyYzZx         ABC123
    
            __yes            star*it        m
             
            me_to-2          main           money$
    
    
  46. Determine the output of the following program segment:
  47.          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);
         
    
    
    
  48. Rewrite the following program, correcting all errors:
  49.      
             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]
         
    
    
  50. Given:

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

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

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

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

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

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

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

        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);
        }
    
    

        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);
        }
    
    

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

        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");
    
    

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

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

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

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

        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;
        }
    
    

        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 );
        }