SampleMidterm-F23 - COMP 2160
SampleMidterm-F23 - COMP 2160
26 October 2023
Instructions:
2. Consider the following lines of code. True or false: the compiler will give
you an error for one (or both) of these lines of code.
char c = ‘a’;
int i = c;
(a) True
(b) False
3. Suppose you have a List data type that is implemented using a linked list.
Which of the following should not be in the interface for this data type?
(a) A function that allocates memory for a list, initializes all the variables in
the associated struct, and returns a pointer to the allocated memory.
(b) A function that inserts a new element at the beginning of the list.
(c) A function that returns the number of elements in the list.
(d) The Node struct used to implement the linked list.
Short Answer
Answer each of these questions in the space provided. (23 marks total)
4. (1 mark) What is the purpose of sizeof when used with malloc? (when is it
necessary?)
5. (1 mark) Why is using the strcpy function (from string.h) to copy a string
dangerous?
6. (7 marks) Name the three kinds of contract terms that are part of a Design
by Contract. Briefly describe where in the program you would check each
of them.
(a)
(b)
(c)
7. (5 marks) Starting with the subset of memory shown below, re-draw the
diagram showing the contents of the variables a, b, c, x, and y after the
code below is executed. Indicate pointers by drawing an arrow from the
memory location associated with the pointer to the location it is pointing
to.
a b c x y
NULL NULL
1 2 3
int a = 1, b = 2, c = 3;
int *x, *y;
x = &a;
y = &b;
*x = *y + 1;
*y = c;
c = *x - *y;
a b c x y
8. (2 marks) Given the definition of the type List and the prototype for the
function append below, give meaningful examples of one precondition and
one postcondition for append. You may either write code (one line for each)
or a brief description of what you are checking.
typedef struct {
double data[100]; //partially filled array
int size; //number of items in data
} List;
(a) precondition:
(b) postcondition:
10. (1 mark) Briefly describe what the preprocessor does when it finds a line
that begins with #include.
Programming
Write your answer as instructed in the space provided. (18 marks total).
You do not need to write comments, main programs, or include or import
statements. If you define any new functions, you do not need to write function
prototypes; you may assume they are present.
Your code must be clearly indented; give variables meaningful names.
13. (6 marks) Given the following struct String, implement the following
function without using any function from string.h. You should implement
all relevant preconditions.
typedef struct{
char *contents;
int length;
} String;
//returns the character at position pos in the String str
char char_at(String *str, int pos)
{
14. (12 marks) Complete the following C function, using good and safe
programming practices. It is passed the name of a file which contains a
list of integers between 0 and 100, one per line. The function should return
the integer that was seen the most, or a -1 if there is an error. You may
assume that the file (if it exists) is formatted correctly, but you should
handle all other potential errors gracefully, by printing an error message
and continuing if possible. Note that your code should not contain “magic
numbers”, you should define constants where appropriate.