html> Exam 1 Review

CMSC 313 Exam 1 Review


Picture ID required for the MidTerm

These questions are study questions. While some of them may appear on the exam, the exam will contain many questions not found here. If you can answer all of these questions correctly, you should do well on the midterm. No code in these questions contains an intentional syntax error. If you believe that you have found a syntax error, please email your instructor.

Don't forget the practice problems listed on the course schedule page


C Programming -True/False Questions

Circle TRUE or FALSE for each of the following questions. If false, explain why.
  1. TRUE/FALSE
    When a function is called its parameters are local to the function.

  2. TRUE/FALSE
    The lifetime of a static variable is the duration of your program's execution.
  3. TRUE/FALSE
    A static global variable may only be referenced in the .c file in which it is defined
  4. TRUE/FALSE
    A static function may only be used in the .c file in which it is defined.
  5. TRUE/FALSE
    The preprocessor directive #include "foo.h" instructs the preprocessor to place the contents of the file foo.h into the code at that point.

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

  7. TRUE/FALSE
    All arrays with 10 elements require the same amount of memory, regardless of the type of elements stored in the array.

  8. TRUE/FALSE
    If p is a pointer to an int, then the expression p[ i ] is invalid unless p is the name of an array.
  9. TRUE/FALSE
    typedef is used to give a new name to a known data type.

  10. TRUE/FALSE
    In C, an argument is passed by VALUE when a copy of its value of is passed to the function's parameter.

  11. TRUE/FALSE
    If rec is a structure variable and number is a member of the structure, then to access the number member of rec use the syntax rec.number

  12. TRUE/FALSE
    The string "Bilbo Baggins", may be stored in an array of 13 characters.

  13. TRUE/FALSE
    The declaration typedef int *intPtr; creates a variable called intPtr which can hold the address of (or point to) an integer.

  14. TRUE/FALSE
    The functions malloc ( ) and calloc( ) are used to dynamically allocate memory.

  15. TRUE/FALSE
    If p is a pointer to structure variable called rec and number is a member of the structure, then to access the number member of rec using p, use the syntax p -> number

  16. TRUE/FALSE
    The preprocessor directives #ifndef, #define, and #endif are used to guard header files.

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

  18. TRUE/FALSE
    The function free() is used to release dynamically allocated memory back to the heap for possible reuse.

  19. TRUE/FALSE
    When an array is passed to a function, a copies of the array elements are made for the function to manipulate.

  20. TRUE/FALSE
    If p is a pointer then the expression
    p = p + 1; adds 1 to the value of p regardless of p's type.
  21. TRUE/FALSE
    If p is a pointer to type int, then the expression &p is a "pointer to a pointer to an int".

  22. TRUE/FALSE
    The declarations char name[10]; and char *name; are identical.

  23. TRUE/FALSE
    If p is a pointer to an int, then the expression &p is invalid and results in a compiler error.

  24. TRUE/FALSE
    If p is a pointer to an int and A is an array of integers, then the statement
    *p = A[0];
    assigns the value stored in A[0] to p.

  25. TRUE/FALSE
    If p is a pointer to integers and A is an array of integers, then the following statements are equivalent:
    p = &A[0];
    p = A;

    C Programming - Multiple Choice Questions

  26. Members of a structure
  27. A function prototype is
  28. Which of the following is the function prototype for a function AlphaBeta that takes two double parameters and does not return any values.
  29. In order to include a header file which is in your current working directory, which of the following would be used ?
  30. Which of the following items belong in a header file?
  31. When an array argument is passed to a function
  32. Let arr be an array of integers, then in the expression arr[i++]
  33. If arr is an array of integers, then a[ i ] is equivalent to
  34. The main difference between a struct and an array is:
  35. The main difference between a struct and a union is:
  36. The expression sizeof(int) refers to
  37. The expression sizeof(struct foo) refers to
  38. The size of a union is
  39. The typedef facility is used to
  40. The length of a string is determined
  41. Given the following declaration,
    	char string[8] = "abcdefg";
    
    what is output by the following statement:
    	printf ("%s\n", string + 3);
    
  42. The following code
    	char string[8] = "abcdefg";
    
    	*string = '\0';
    	printf ("%s", string);
    

  43. Which of the following declares a pointer variable (ptr) and initializes it to the address of x?

  44. What is the effect of dereferencing a pointer which has the value NULL ?

  45. What is the advantage of using a pointer to a structure as a parameter to a function, instead of the structure itself ?

  46. During file processing, a file is referenced via what type of variable ?

  47. When using command line arguments, what is the content of argv[0] ?

  48. Given the array declaration int array[10]; which of the following is a pointer to the second element of array.

    C Programming - 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.
  49. Write a function called ArrayMin that returns the minimum value in an array of n integers, where array and n (the size of the array) are parameters passed to the function.
  50. Give an example of how ArrayMin would be called from main.
  51. Given the structure definition/declaration below, write a printf() statement which prints today in the form month/day/year.
        struct date
        {
            int year;
            int month;
            int day;
        } today ;
    
  52. 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 Mickey(int, int) ;
    int Mouse(int) ;
    
    int main()
    {
        int a = 2, b = 3, c = 3 ;
    
        a = Mickey (b + 5, c) ;
        b = Mouse (c) ;
    
        printf ("main: a = %d, b = %d, c= %d\n", a, b, c) ;
    
        return 0 ;
    }
    
    int Mickey (int x, int y)
    {
        int z ;
    
        z = x - y ;
        printf ("Mickey: x = %d, y = %d, z = %d\n", x, y, z) ;
    
       return (z) ;
    }
    
    
    int Mouse (int c)
    {
        int a, b ;
    
        a = 1 ;
        b = c * 2 + a ;
        a = b + 5 ;
        c = Mickey (a, b) ;
    
        printf ("Mouse: a = %d, b = %d, c = %d\n", a, b, c) ;
    
        return (b) ;
    }
    
  53. Fill in the blanks below. The function FindNet computes a worker's weekly net pay given hours (an integer), rate (a double), and exempt (an int used as 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, ________ ______ , bool _______ )
    
    {
        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);
    
    
        ________ ( __________ );
    }
    
    
  54. Describe what the function below does. The parameter a is 2-dimensional array of positive floating point numbers.
    float Bob (float a[10][5])
    {
        float x = 0.0;
        int i, j;
    
        for (i = 0; i < 10; i++)
        {
            for (j = 0; j < 5; j++)
            {
                if (a[i][j] > x)
                {
                    x = a[i][j];
                }
            }
        }
    
        return x;
    }
    
  55. What common bug is found in the code fragment below ?
                int a[5];
    
                for(i = 1; i <= 5; i++)
                {
                    a[i] = 0;
                }
    
  56. Write a function called Scan(), which takes a string and a character as its two parameters and returns an integer which is the number of times the given character appears in the string.
  57. Write a function CountVowels() that examines a string and returns the total number of vowels in that string (a, e, i, o, u).
  58. Write a definition for a struct named book which contains three members:
    1. a string title of at most 50 characters
    2. a string author of at most 50 characters
    3. an integer variable checkedOut.
  59. What happens if your program tries to access an array element with an out-of-range subscript ?

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

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

  61. Show two ways to obtain the address of the first element of the array data[].

  62. Show two ways to assign the value 100 to the third element of the array named data[].

  63. What is the output of the following code ?
        int m = 5, n = 6, *p, *q;
        
        q = &n;
        p = &m;
        *p = *q * 2;
        printf ("%d %d %d %d\n", m, n, *p, *q);
    

  64. Write a function called GetCoordinates() which prompts the user to enter three coordinates, x, y, and z. All three coordinates are of type float. These values are "returned" via the functions parameters using call by reference of the three values.

  65. Write a single statement that opens the file "oldmast.dat" for reading and assigns the returned file pointer to the variable ofPtr.
  66. Given the following structure definition
        
        typedef struct person
        {
            char name[50];
            int  age;
        } PERSON;
    
    and given the following code fragment:
        struct person Bob;         /* line 1 */
    
        scanf ("%s", Bob.name);    /* line 2 */
        scanf ("%d", &Bob.age);    /* line 3 */
    
    explain why the call to scanf ( ) in line 3 requires the 'address of' operator, but the call to scanf ( ) in line 2 does not. BE SPECIFIC

  67. Starting with PERSON from above
    1. Modify PERSON by adding a pointer to another PERSON.
    2. Declare a pointer named me to be a pointer to a PERSON.
    3. Allocate dynamic memory to which me points.
    4. Write code to input my name and age using scanf( )
    5. Define another PERSON named sister then write code to input my sister's name and age, and create a link between me and my sister. Be sure to indicate that we have other siblings.
  68. Write a program that takes several command line arguments and then prints those strings to the screen.

  69. What is a memory leak ?

  70. What is a dangling pointer ?


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


  71.    char string[10] = "abcdefgh", *p = string, ch;
    
       while (*p != '\0')
       {
          ch = *p++;
          *p++ = ch;
       }
       printf ("%s\n", string);
    


  72.    char string[15] = "This is a test";
    
       strcpy (string + 10, "cat");
       printf ("%s\n", string);
    

  73. Describe each of the steps taken by gcc to create an executable program from a .c source file
  74. Define a recursive function and give a short example.
  75. What is the meaning of the following printf conversions -- %d, %x, %p, %s, %f
  76. What is the difference between using scanf( "%s", string); and fgets( ) to read input from the user?
  77. Explain why it is necessary to use the C string library function strcmp( ) rather than using the equality operator ==.
  78. What common programming error is found in the following code?
          char name[20];
          printf("%s", "Please enter your name\n");
          scanf( "%s", name);
    
  79. Draw the picture of memory that results from the following code
        int i;
        char *names[5];
        
    	for (i = 0; i < 5; i++)
    	    names[i] = NULL;
    	names[0] = (char *)malloc(10 * sizeof(char));
    	names[2] = (char *)malloc(12 * sizeof(char));
    	names[4] = (char *)malloc(5 * sizeof(char));
    
    	strcpy(names[0], "Bob Smith");
    	strcpy(names[2], "Jimmy Smith");
    	strcpy(names[4], "Tom");
    
  80. Consider the declarations below.

    char *boysNames[5];
    char **girlsNames;

    Explain the difference between these declarations.
    Draw the pictures of memory that each represensts.
    Write code to store the names "Bobby", "James", "Bill", "Thomas" and "Sam" into boysNames
    Write code to store the names "Susan", "Rachel", "Laura", "Michele" and "Liz", "Joanne" and "Samantha" into girlssNames

  81. What is the purpose of assert( ). Give a short example of its use