UMBC CMSC202, Computer Science II, Fall 1998,
Sections 0101, 0102, 0103, 0104
Sample Exam
Warning: Do not study from this sample exam
By request, I have posted this sample exam so you can look at the format of
the exam. This exam was given in CMSC 202 in the Spring 98 semester. That
class followed a very different syllabus, so you should not study this
exam for content.
Instructions:
- This is a 75-minute, closed-book, closed-notes exam.
- Calculators are not allowed --- you won't need them.
- Print your name, fill in your student identification number, and check
the discussion section where you would like to receive the graded exam.
True-False Questions: 1 point each
Circle one of TRUE or FALSE after each question.
- Mergesort is much faster when the input is already sorted than when the
input is in random order.
TRUE FALSE
- The binary search algorithm is best described as an O(n) algorithm.
TRUE FALSE
- The function 7 n2 + 10 n log(n) is O(n log(n)).
TRUE FALSE
- If p is a pointer to int then the statement
p = (int *) malloc(152) ;
allocates enough memory for an array of 152 int's.
TRUE FALSE
- The function free() is used to determine the largest block of memory
that malloc() can currently reserve.
TRUE FALSE
- In C, a pointer to void always points to NULL.
TRUE FALSE
- The "list" abstract data type must be implemented using
singly- or doubly-linked lists.
TRUE FALSE
- In a circular linked list, the "next" field of the last node
points to the "header" node.
TRUE FALSE
- When a call to malloc() returns NULL because the system does not have
enough memory, the situation is called a "memory leak".
TRUE FALSE
- C uses a "garbage collector" to recover dynamically allocated
memory that is not used any more.
TRUE FALSE
Multiple Choice Questions: 2 points each
For each question below, circle the best answer.
- Suppose that the C expression p[i] has type int. Then the type of p can
be:
- int
- pointer to int
- pointer to an array of int
- none of the above
- Suppose that sizeof(int) is 4 and that the address value stored in the
int pointer ptr is 4294946208. Then after the statement ptr++ ; the address
value stored in ptr is:
- 4294946209
- 4294946212
- 4294946224
- any of the above
- For programs running under UNIX, a "segmentation fault" occurs when
- A program tries to access memory reserved for the operating system
- A program tries to access memory reserved for another user
- A program tries to dereference a NULL pointer
- all of the above.
- When we call realloc() to increase the amount of memory allocated for
an array, we follow a strategy that doubles the size of the array instead
of increasing the size of the array by 1 because:
- this strategy reduces the amount of time that the system might spend
copying.
- this strategy reduces the amount of memory used by the program.
- this strategy reduces the probability of a segmentation fault.
- all of the above
- In a C file, after the type declaration
typedef struct tag {
struct tag * foo ;
double r ;
} bar ;
- the field foo is a record of type bar.
- the field foo is a type.
- the field foo is a pointer to a record of type bar.
- the field foo is a pointer to a linked list.
- Consider the following declarations:
typedef struct node_tag {
char *item ;
struct node_tag *next ;
} node ;
typedef struct {
node header ;
int count ;
node *last ;
} *list ;
list L ;
Which of the following expressions has type char, assuming that all the
necessary memory has been allocated?
- L.header.item[3]
- L.header->item[3]
- L->header.item[3]
- L->header->item[3]
- The purpose of the "header" node in a singly-linked list is:
- to keep track of the last node in the linked list
- to keep track of the first node in the linked list
- to keep track of the number of elements in the linked list
- to simplify the C code for inserting new nodes into the linked list
- Each node of a doubly-linked list contains one more pointer than a
singly-linked list. This extra pointer is used to:
- point to the previous node in the linked list
- point to the "header" node
- point to the last node in the list
- none of the above
- An advantage of linked lists over dynamically allocated arrays is:
- linked lists generally use less memory
- the size of a dynamically allocated array must be known when the
program is compiled
- inserting at the beginning of a linked list is faster than inserting at
the beginning of an array
- all of the above
- An advantage of dynamically allocated arrays over linked lists is:
- arrays generally use less memory
- C code for arrays is somewhat simpler than C code for linked lists
- binary search can be used with a sorted array, but not with a sorted
linked list
- all of the above
Short Answer Questions: 4 points each
In the following questions, no syntax errors have been 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.
- Consider the following recursive function:
int foobar (int n) {
if (n == 0) return 0 ;
if (n < 0) return foobar(-n) ;
return foobar(n-1) + 2*n - 1 ;
}
- What value is returned by foobar(-3) ?
- What value is returned by foobar(4) ?
- What value is returned by foobar(5) ?
- Describe in less than 10 words the mathematical function that the
function foobar() computes.
- Write a recursive function with the function prototype
int IsSorted(int A[], int low, int high) ;
Your function must determine whether the array A is sorted in increasing
order between indices low and high inclusively. If the array is so sorted,
your function must return the value 1; otherwise the return value must be
0. Your function must be recursive and must implement the following
recursive strategy:
An array is sorted in increasing order if and only if the first element is
less than or equal to the second element and the array without the first
element is sorted in increasing order.
Note: the statement above does not cover any base cases.
- Write a function MakeFloatArray() which takes an integer parameter n
and returns a pointer to float. This pointer must point to the first
element of an array of n elements, each of which has type float. Memory for
the array must be dynamically allocated. If memory allocation fails, your
function should return a NULL pointer. Finally, each element of the array
must be initialized to the value 1.0.
- Two-dimensional arrays. Write down the output of the following program.
Note: this program has been compiled and runs without causing a
segmentation fault or bus error. Answers that say "segmentation
fault" or "bus error" will receive zero credit.
#include :
typedef int array[4] ;
main () {
array SA[4], *aptr ;
int i, j, *iptr ;
for (i = 0 ; i < 4 ; i++) {
for (j = 0 ; j < 4 ; j++) {
SA[i][j] = 10 * i + j ;
}
}
aptr = &(SA[2]) ;
iptr = &((*aptr)[0]) ;
for (i = 0 ; i < 4 ; i++) {
printf("%d ", *(iptr + i) ) ;
}
printf("\n") ;
}
- Write the type definitions, variable declarations, memory allocation
statements and assignment statements that will construct the following data
structure. All boxes that contain numbers have type int. All boxes that
have arrows coming out of them are pointers. Except for memory for the
variable ptr, memory for all boxes must be dynamically allocated. Thus, ptr
is a pointer to a record whose second element is a pointer to int which
points into an array of 4 elements, each of which is an int. If you
have doubts about the type of a particular box, you may ask a proctor for a
brief explanation.
Last Modified:
22 Jul 2024 11:28:52 EDT
by
Richard Chang
Back up
to Fall 1998 CMSC 202 Section Homepage