NAME
General instructions:
There are problems worth a total of points . Read the problems very carefully. Identify what
kind of answer the problem asks for. Carefully look at requirements for input and output,
and any restrictions on how your code must be written.
You may assume that input will not produce errors for the procedures described, unless
the questions says otherwise.
Do problems you are confident about first. If you finish the problems you know, write
what you do know about other problems to gain partial credit; but erroneous information
may detract from that credit or irritate the grader, so don’t make stuff up.
Do not do unnecessary testing. For example, testing for both x < 0 and x >= 0 instead
of using one test and then else would be considered unnecessary testing.
Do not make code unnecessarily inefficient.
Short answer. Use only the space provided.
1. What values are true in C?
2. What does the C statement “int q;” do inside the computer, as discussed in class?
3. Is #include a reserved word in C? Explain your answer very briefly.
4. When would you use a sentinel-controlled loop?
5. (6 pts) Write two complete examples, one of each of the preprocessor directives we have
reviewed in class.
6. What is the main issue, as discussed in class, that you must consider when choosing a
sentinel?
7. Structured programming says that all programs can be written using a combination of
three kinds of control structures. What are they?
8. You are told to write some code that will execute 100,000 times. As discussed in class,
which type of loop should you probably use?
, 9. You are told to write a sentinel-controlled loop. As discussed in class, which type of
loop should you probably use?
10. You are told to write a loop that runs until the user enter a Carmichael number. As
discussed in class, which type of loop should you probably use?
11. (6 pts) Write two complete examples, one of each of the preprocessor directives we have
reviewed in class.
12. (4 pts) When you load a C program, what physical part of the computer does it come
from?
13. (6 pts) What are the structures we have studied in class so far for accomplishing
selection of code in C?
14. (12 pts) Write the appropriate shell command next to each task:
(a) compile a C program
(b) list the contents of a file
(c) show the location of the current directory
(d) create a new directory
(e) make a copy of a file
15. (4 pts) What is binary 1001001101011010 in hex?
16. (4 pts) What is binary 1101101 in decimal?
17. (4 pts) What is decimal 567 in hex?
18. (4 pts) What is decimal 322 in binary?
, 19. (4 pts) Advocates of this philosophy proved that all programming could be done using
only sequence, selection, and repetition. What is its name?
20. (4 pts) Write the binary equivalent of the decimal number 157.
21. (10 pts) Write a for loop using the variable provided that prints the following output:
% a.out
2 5 8 11 14
%
int i;
22. 12 pts. Write the six stages of a C program in order. Next to each, write the shell
command that accomplishes it.
For the next section, choose the letter that shows the value that would be in x. Assume
x and y are declared as integers. Choose “error” if you think a fragment will not
compile and run. The fragments are unrelated (each question is separate from the
others). HINT: Calculate your answer before you look at the selections.
23. (2 pts)
x = 2 - 3 * 2 - 7;
(a) 5 (b) 11 (c) 2 (d) error (e) none of the above
24. (2 pts)
x = 2 + 3 || 2 < 3;
(a) 5 (b) 0 (c) 1 (d) error (e) none of the above
25. (2 pts)
y = 3.6;
x = (double)y * 2;
(a) 7.2 (b) 7 (c) 6 (d) error (e) none of the above
26. (2 pts)
x = 15 % 3;
(a) 5 (b) 1 (c) 3 (d) error (e) none of the above