C Basics: Types, Control and Functions · Multiple choice · 1 mark
In C17, what are the values of -7 / 2 and -7 % 2, in that order?
and
and
and
and
Answer: (D)
Integer division truncates toward zero, so -7 / 2 is . The remainder always satisfies (a / b) * b + a % b == a, so -7 % 2 is : the remainder takes the sign of the dividend. Rounding down to (floor division, as in Python) is not what C does.
Pointers, Arrays and Pointer Arithmetic · Write code · 2 marks
Write the function below. Walk the range with a pointer; do not use [] or any library function.
// sum_range(begin, end) returns the sum of the ints in the half-open range
// [begin, end): begin points at the first int, end one past the last.
// An empty range (begin == end) has sum 0.
// requires: begin and end point into the same array, begin <= end
int sum_range(const int *begin, const int *end);int sum_range(const int *begin, const int *end) {
int total = 0;
for (const int *p = begin; p < end; p++) {
total += *p;
}
return total;
}One mark for starting at begin, one for stopping with p < end (never dereferencing end), one for the accumulator and the empty-range case.
C Strings and Buffers · Multiple choice · 1 mark
What can be said about this fragment?
char *p = "abc";
p[0] = 'x';
printf("%s\n", p);It prints xbc.
It prints abc; the assignment is silently ignored.
It does not compile, because p must be declared const char *.
The behaviour is undefined, because the assignment modifies a string literal.
Answer: (D)
(D). A string literal may be stored in read-only memory and modifying it is undefined behaviour; on Linux it normally crashes with a segmentation fault, but the standard promises nothing. It compiles because the type of a literal in C is char [4], not const char [4]. Declaring char p[] = "abc"; makes a writable copy.
The Stack, the Heap and Dynamic Memory · Multiple choice · 2 marks
The function make is called as int *p = make(); and the caller then reads *p. What is wrong?
int *make(void) {
int x = 7;
return &x;
}The address of a local variable cannot be taken in C.
x ceases to exist when make returns, so p dangles.
It should return *x, because functions return values, not addresses.
It leaks memory, because x is never passed to free.
Answer: (B)
x lives in make’s stack frame, which is reclaimed on return. The caller receives the address of dead storage; reading it is undefined behaviour. Returning the value, or a pointer to a malloced block (which the caller frees), fixes it. Locals are never freed by hand, so there is no leak.
Structs and Linked Structures · Write code · 3 marks
Write the function bst_insert described below, recursively. You may assume that malloc succeeds.
struct tree {
int key;
struct tree *left;
struct tree *right;
};
// bst_insert(t, k) inserts the key k into the binary search tree t and
// returns the root of the resulting tree. If k is already in the
// tree, the tree is unchanged.
// requires: t is NULL or the root of a valid binary search tree
// effects: allocates memory
struct tree *bst_insert(struct tree *t, int k);struct tree *bst_insert(struct tree *t, int k) {
if (t == NULL) {
t = malloc(sizeof *t);
t->key = k;
t->left = NULL;
t->right = NULL;
} else if (k < t->key) {
t->left = bst_insert(t->left, k);
} else if (k > t->key) {
t->right = bst_insert(t->right, k);
}
return t;
}Computer Fundamentals · University of Toronto (UofT, U of T, UToronto) · also written APS105, APS 105
Make a free, printable APS105H1 practice exam: choose the topics, the length and the difficulty above, and get an exam-style PDF with a full answer key. Each paper draws on 250 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the UofT library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh APS105H1 practice exams here on the same topics, with full answer keys, as many as you need. The questions here are original, not copies of past exams.
questionbank.dev is independent and not affiliated with University of Toronto. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.