Sample questions for midterm exam

CSC 7000-002 Algorithms & Programming
Fall 1999, Mendel 115
Dr. David Matuszek

The following questions are intended to be fairly representative of the ones that will be on your midterm exam. In fact, a few of them may even be on the midterm exam. Note that almost all of them are answered, directly or indirectly, by the "Concise Introduction to C" paper; a few, however, are drawn from class discussions.

1. Circle the words that are keywords in C:

    break  single  double  value  short  long   function  deftype

2. True or false: for each function, you should supply a comment describing how the function works.

3. What character sequence do you use to put a "newline" in a string? _____________

4. Strings in C are always terminated by a ______________________.

5. Given the declaration char* p, q;
        (a) what is the type of p?

        (b) what is the type of q?

6. If a is an array, a[i] is equivalent to the pointer expression __________________________.

7. Fix the syntax error in this definition: struct complex { double re; double im }

 

8. Given the (corrected) definition in question 7, write a line to declare origin to be a complex number.

 

9. If you put the declaration of a variable x outside any of your functions, what is the scope of x?

 

10. If result is a variable of type int, and score is a variable of type double,then the following statement has an error in it. Write the corrected statement.

   result = 100 * score;

 

11. Assuming that i, j, and k are all integers, what value gets assigned to k by:

   i = 3; j = 5; k = ++i * j++;

 

12. You should be familiar with the functions sqrt(), malloc(), and rand(). Write the prototype for each of these functions.

 

 

13. Write an integer function max that returns the larger of its two (integer) arguments.

 

 

 

14. Suppose you have a function swap that takes two integer arguments and interchanges their values. How would you write a call to this function to swap the values of integer variables best and worst? (Notice that this function changes the values of its parameters!)

 

15. Our function main actually has arguments, but most of our programs so far have ignored those arguments. Write a prototype for main that shows what the arguments actually are.

 

16. What UNIX command will tell you all about the different predefined string functions you can use?

17. What conversion specs would you give to scanf() in order to write out:
    (a) an integer?
    (b) a floating point number with no exponent?
    (c) a single character?
    (d) a string?
    (e) a hexadecimal integer?

18. Write all the statements necessary to read a single integer from a file accountinfo into an integer variable passcode. Don't try to write a complete program, just the statements relevant to reading this one integer.

 

 

 

19. What statement can you use to get out of a loop "early" (before the loop would normally finish)?

20. Tell one place where you need a semicolon after a close brace, that is,  };

 

 

Short answer:

21. What is the difference between stdout and stderr?

 

 

22. What is a "memory leak"?

 

 

23. If your program runs but produces incorrect results, what is the best strategy for finding the error?

 

 

24. Why is good indentation important?

 

 

25. What good are functions? That is, how does defining your own functions make your programming easier?