CMPSC
311,
Spring 2013, Midterm Exam 1, Sample questions for review
The first midterm exam will be Tuesday, Feb. 12, 6:30 - 7:45 pm, in
112 Kern Bldg.
Anyone who has a conflict with the scheduled time, especially
another
class or exam the same night, should have asked about a conflict
exam
already. Time and place information has been sent to you by
email.
The exam is closed book, closed computer, closed neighbor, no cell
phones, etc.
But, you can bring one 8 1/2 x 11 sheet of paper, with your name on
it,
as a "cheat sheet"; you must turn this in with the exam.
Class time on Monday, Feb. 11, will be devoted to review only.
Project solutions are posted on ANGEL.
The exam will ask questions about general knowledge of C and Unix,
and
will require both programming and debugging. Any material that
was covered in class, as assigned reading, as background for the
projects, or in the
projects, could be on the exam. In particular, don't neglect
the
programming examples in the Intro
to
Unix notes, or the exercises in the notes. The "cheat
sheet"
could remind you of Unix function prototypes and C syntax,
but you should be able to answer most questions without referring to
it.
The questions will be
- True/False [20 items, 1 point each]
- Multiple Choice [15 points, five questions at 3 points
each,
partial credit possible]
- Programming [45 points, three questions at 15 points
each]
- write a function or a code sequence to ...
- It would be a real help to have done the projects and
studied
the examples from class and the lecture notes.
- Debugging [20 points, two questions at 8 and 12 points]
- explain what's wrong, and how to fix it (in English and C)
The number of points in each category might be different, but not by
much.
Some of the sample questions given here are harder than are actually
on the exam.
1. True/False, circle T or F. [1 point each]
T F (a) The Posix Standard describes only
an
interface to the operating system, as a set of functions callable
from
C.
T F (b) When a Unix process starts, the
input
and output streams stdin, stdout and stderr
are opened.
T F (c) When a C or Unix library function
fails, it sets the global variable errnum to indicate
the
reason for
the failure.
T F (d) The assert() macro
can
be used to verify a boolean expression at runtime; if the expression
evaluates to false, the program is terminated.
T F (e) Parameter-passing in C is always
call-by-value (or, pass-by-value, which is the same thing).
T F (f) Reference parameters (from C++) can
be
approximated by pointer parameters in C.
2. Multiple Choice [3 points each]
Choose only one of the four given answers in each part, by circling
1,
2, 3, or 4. There is at least one correct answer given in each
part, and there may be other correct answers. Partial credit
is
possible for some incorrect answers.
(a) A file descriptor is
(1) a non-negative integer obtained from a
call to open().
(2) a non-negative integer obtained from a
call to fopen().
(3) a pointer obtained from a call to open().
(4) a pointer obtained from a call to fopen().
(b) Comparing system functions and library functions, ...
(1) system functions are provided with the
operating system, while library functions are provided with the
programming language.
(2) library functions are provided with the
operating system, while system functions are provided with the
programming language.
(3) there is no difference, they are both
provided with the operating system.
(4) there is no difference, they are both
provided with the programming language.
(c) An operating system would be expected to ...
(1) provide methods for connecting hardware
and software components.
(2) manage resources.
(3) provide abstractions of the resources.
(4) map virtual resources to actual
resources
over time.
3. [20 points] Design and write a function that will
estimate the
size of the largest single block of memory that can be successfully
allocated with malloc().
Do
this
efficiently.
Pseudocode
is
acceptable as long as it's not
"too pseudo". It would help in assigning partial credit if you
also give a diagram of memory
allocation within a process address space, to help explain how your
function
works.
"Cheat sheet" info - malloc() is given one
argument, a number of bytes to allocate. If there is enough
space
available in the process heap segment, malloc()
returns a
non-NULL
pointer to that space, otherwise it returns NULL. The function
free()
takes one argument, which is expected to be a return value from malloc(),
and
deallocates
that
space.
malloc() does not
initialize the memory it allocates.
4. [8 points] Consider the swap function
static inline void
int_swap(int
*a, int *b)
{ int t = *a; *a = *b; *b = t; }
Explain what is wrong with each of the following uses of swap().
(a) int m, n; int_swap(&m, &n);
(b) int_swap(m, n);
(c) int_swap(&m, &3); // assign 3 to m
5. [lots of points] This problem has happened to several
students. The program seems to run but it produces no
output. Explain why.
6. [10 points] Write a simple version of the od
program.
"Cheat sheet" info - See the Course
Intro notes and the Project 2 and 3
descriptions for
examples. The actual test would include a short example, since
od
has several options, and we need to be definite about which one to
use.
7. [6 points] Explain the errors and repair the code.
int size_intarray =
... some
value from the input ... ;
int *intarray =
malloc(size_intarray);
for (i = 0; i <
size_intarray; i++)
{ total +=
intarray[i]; }
8. [8 points] Explain the errors and repair the code.
pid_t pid;
int status;
if ((pid = fork()) = 0)
{ /* child */
execlp("bin/ls", "bin/ls", NULL);
}
else
{ /* parent */
waitpid(pid, &status, 0);
}
9. [8 points] Indicate what is wrong with the following
C
program, and correct the program. (The return type of foo()
is intended, so don't change that.) (Hint:
parameter
passing, not malloc)
1 #include
<stdlib.h>
/* for malloc(), free() */
2 /* This function acquires memory
and
returns the
3 * address to
the
caller via the parameter target.
4 * The memory can be released
by
calling free().
5 */
6 void foo(char *target)
7 {
8 void * buf =
malloc(sizeof(char) * 10);
9 (*target) = buf;
10 }
11 int main(void)
12 {
13 char *line;
14 foo(line); /*
allocate memory */
15 free(line); /*
deallocate memory */
16 }