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: [Underlined: not easy to circle words in HTML!]
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.
False. (You should have a comment describing what the function does, not how it works.)
3. What character sequence do you use to put a "newline" in a string?
\n
4. Strings in C are always terminated by a zero byte.
5. Given the declaration char* p, q; (a) what is the type of
p?
char*
(b) what is the type of q?
char
6. If a is an array, a[i] is equivalent to the pointer
expression *(a + i) .
7. Fix the syntax error in this definition: struct complex { double re; double im
}
As written, there appear to be two errors (sorry!):
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.
struct complex origin;
9. If you put the declaration of a variable x outside any of your
functions, what is the scope of x?
From the point of declaration to the end of the file.
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;
result = (int) (100 * score); /* Correct
answer */
result = 100 * (int) score; /* Legal
but probably incorrect */
11. Assuming that i, j, and k are all integers,
what value gets assigned to k by:
i = 3; j = 5; k = ++i * j++; 20
12. You should be familiar with the functions sqrt(), malloc(),
and rand(). Write the prototype for each of these functions.
double sqrt(double)
void *malloc(int)
int rand(void)
13. Write an integer function max that returns the larger of its two
(integer) arguments.
int max (int a, int b) {
if (a > b) return a;
else return b;
}
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!)
swap (&best, &worst);
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.
int main (int argc, char *argv[]);
16. What UNIX command will tell you all about the different predefined string functions you can use?
man stringor one ofman strstr, man strlen, ...
17. What conversion specs would you give to scanf() in order to write out:
(a) an integer? %d
(b) a floating point number with no exponent? %f
(c) a single character?
%c
(d) a string? %s
(e) a hexadecimal integer?
%x
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.
FILE *myfile;
myfile = fopen ("accountinfo", "r");
fscanf(myfile, "%d", &passcode);
fclose (myfile); /* optional */
19. What statement can you use to get out of a loop "early" (before the loop
would normally finish)? break;
20. Tell one place where you need a semicolon after a close brace, that is,
};
After an
enumdeclaration.
Short answer:
21. What is the difference between stdout and stderr?
By convention,
stdoutis used for normal program output, whilestderris used for error messages.
22. What is a "memory leak"?
When a program allocates memory (using
mallocor similar), then loses track of that memory without freeing it to be reused.
23. If your program runs but produces incorrect results, what is the best strategy for finding the error?
Think about what the program might have done to produce those results.
24. Why is good indentation important?
It makes it easier to see which statements are controlled by which other statements. No indentation or (worse) misleading indentation makes it much harder to see the scope of loops, etc.
25. What good are functions? That is, how does defining your own functions make your programming easier?
They allow you to break up a large programming task into small, easy to understand pieces.