CMSC 313 Midterm Exam Review
Fall 2009


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 parameter 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, ________ ______ , 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); ________ ( __________ ); }
  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 four 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 struct person { char name[50]; int age; }; 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. Write a program that takes several command line arguments and then prints those strings to the screen.

  68. What is a memory leak ?

  69. What is a dangling pointer ?


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


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


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

  72. Describe each of the steps taken by gcc to create an executable program from a .c source file
  73. Define a recursive function and give a short example.
  74. What is the meaning of the following printf conversions -- %d, %x, %p, %s, %f
  75. What is the difference between using scanf( "%s", string); and fgets( ) to read input from the user?
  76. Explain why it is necessary to use the C string library function strcmp( ) rather than using the equality operator ==.
  77. What common programming error is found in the following code? char name[20]; printf("%s", "Please enter your name\n"); scanf( "%s", name);
  78. 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");
  79. What is the purpose of assert( ). Give a short example of its use

  80. Bits, Bytes and Integers -- Short Answer

  81. What's the difference between a program and a process?

  82. Convert the following decimal values to hexadecimal
    1. 123456
    2. 313
    3. 42
    4. 109384
  83. Convert the following hex values to decimal and binary
    1. 0x313
    2. 0x42
    3. 0x1234
  84. Convert the following binary values to hex
    1. 010011101101
    2. 01101110010000
    3. 1010110110
  85. What does the C operator sizeof( ) do, and why is important for writing portable C code?
  86. What is the role of Virtual Memory with respect to executing a program?
  87. What is the highest memory address available on a machine with a 32-bit word size>
  88. What is meant by the terms big Endian and Litte Endian?
  89. What is the largest positive number that can be stored using 2's complement (TMax) in an integer of word size w.
  90. What is the smallest negative number that can be stored using 2's complement (TMin) in an integer of word size w.
  91. Let A = 0110000110, and B = 0011011000, both in binary. What is the value of each of the following C expressions?
    1. A | B
    2. A & B
    3. ~A
    4. ~B
    5. !B
    6. A ^ B
    7. A & 0xFF
    8. B | 0xab
    9. A && B
    10. A || B
    11. A << 3
    12. B >> 4
  92. List all "integral" data types in C
  93. Define the terms arithmetic right shift, logical right shift, sign extension, overflow

  94. What possible problems may result from converting an unsigned int to a signed int and vice-versa.
  95. What possible problems may result from converting an int to a short?
  96. Define overflow, negative overflow, positive overflow as they relate to arithmetic operations
  97. Describe a simple technique for performing 2's complement negation.
  98. Explain how bit shifting can be used to perform integer division and multiplication.
  99. Find the decimal values of the following 8-bit binary numbers.
    1. 1000 0001
    2. 0111 1011
    3. 1111 0001
    4. 0010 1010
  100. Perform 2's comlement addition on each pair of 8-bit binary numbers. In each case, indicate whether an overflow has occured, assuming an 8-bit word sizd.
    1. 1110 1001 + 0110 1001
    2. 1110 1011 + 1111 1111
    3. 1000 1100 + 1100 0001
    4. 0111 1001 + 0000 1001
  101. Complete the following table, assuming a 4-bit word size
    Hex ValueBit RepresentationUnsigned ValueSigned Value
    5   
    8   
    A   
    C   
    F   
  102. Assume your program is running on a 6-bit machine using 2's complement arithmetic for signed integers. A short integer is encoded using 3 bits. Fill in the empty boxes in the table below. The following declarations are used in the table short sy = -3; int y = sy; int x = -17; unsigned ux = x;
    ExpressionDecimal ValueBinary
    Zero0 
    n/a-6 
    n/a 01 0010
    ux  
    y  
    x >> 1  
    TMax  
    -TMin  
    TMin + TMAX  
  103. Assume your program is running on a 32-bit machine with 32-bit integers represented in 2's complement and that integers are right shifted arithmetically. Value of type unsigned int are also 32 bits. Consider the following declarations int x = random( ); /* some random integer */ int y = random( ); int z = random( ); unsigned ux = (unsigned) x; unsigned uy = (unsigned) y; For each of the C expressions in the table below, indicate whether or not the expression always evaluates to true. If not, explain why not
    Expression
    (x < y ) == (-x > - y) 
    ((x = y) << 4) + y - x == 17 * y + 15 * x 
    ~x + ~y + 1 == )~)x + y) 
    ux - uy == -(y - x)  
    (x >= 0) || (x < ux) 
    ((x >> 1) << 1 <= x