0% found this document useful (0 votes)
18 views8 pages

SampleMidterm-F23 - COMP 2160

The document is a midterm exam for COMP 2160 Programming Practices, dated 26 October 2023. It includes instructions for answering questions, a multiple-choice section, short answer questions, and programming tasks related to C programming concepts. Students are required to write legibly and use appropriate technical terms, with no external resources allowed during the test.

Uploaded by

Raghav Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views8 pages

SampleMidterm-F23 - COMP 2160

The document is a midterm exam for COMP 2160 Programming Practices, dated 26 October 2023. It includes instructions for answering questions, a multiple-choice section, short answer questions, and programming tasks related to C programming concepts. Students are required to write legibly and use appropriate technical terms, with no external resources allowed during the test.

Uploaded by

Raghav Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

COMP 2160 Programming Practices Midterm (A01)

26 October 2023

Instructions:

• Answer all questions in the space provided.


• Use appropriate technical terms in your written answers.
• No calculators or other devices or resources are permitted during the
test.
• Write your name and student number, in the boxes provided, on this
page only.
• Write legibly. Answers that are unreadable cannot be marked.
• Write visibly. Use a dark ink pen or soft-leaded pencil (HB or softer) and
adequate pressure to ensure your answers will be scanned correctly.
• There is extra space for rough work on the last page.
Multiple Choice
Circle exactly one of the options under each question/statement. (3 marks, 1
mark each)

1. When using a source-level debugger, a breakpoint is:


(a) a program that we can use to find and fix bugs in our code.
(b) a place in the program where execution will be stopped.
(c) a tool that tells us where a crash is happening.
(d) the point where you have a breakdown from debugging for too long.

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;

//append value to the end of list


void append(List *list, double value);

(a) precondition:

(b) postcondition:

9. (1 mark) Given a function that finds the minimum value of an array of


integers, describe the input you would use to test an edge case for this
function.

10. (1 mark) Briefly describe what the preprocessor does when it finds a line
that begins with #include.

11. (1 mark) Why might it be better to pass a pointer to a struct to a function,


rather than just passing the struct, even if you are not modifying its
contents?
12. (4 marks) Briefly describe two ways in which C arrays are different than
Java arrays. Do not include syntactic differences (like the position of the
square brackets!).

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.

int int_counter(char *file_name)


{
Rough work/extra space
Rough work/Extra space

You might also like