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.
C Strings and Buffers · Multiple choice · 1 mark
Assume 64-bit pointers. What does this program print?
#include <stdio.h>
void f(char s[]) {
printf("%zu\n", sizeof s);
}
int main(void) {
char a[10] = "hi";
f(a);
return 0;
}8
10
3
2
Answer: (A)
(A). A parameter declared char s[] is really char *s, so sizeof s is the size of a pointer, 8 bytes. The array size 10 is only visible where a is declared; 3 and 2 are the string’s size with terminator and its length.
Structs and Linked Structures · Find the bug · 3 marks
The function person_create should return a new struct person holding a copy of name. It compiles but contains two bugs, and a program that uses it fails on some inputs. Assume malloc succeeds.
struct person {
char *name;
int age;
};
struct person *person_create(const char *name, int age) {
struct person p;
p.name = malloc(sizeof(name));
strcpy(p.name, name);
p.age = age;
return &p;
}Identify the two bugs by line number and say what goes wrong at run time. [2]
Rewrite the function correctly. [1]
Line 8: sizeof(name) is the size of a pointer (8 bytes), not the length of the string, so a name longer than 7 characters overflows the heap block (and the terminator needs strlen(name) + 1 in any case). Line 11: &p is the address of a local variable; p is gone when the function returns, so the caller receives a dangling pointer to the stack.
struct person *person_create(const char *name, int age) {
struct person *p = malloc(sizeof *p);
p->name = malloc(strlen(name) + 1);
strcpy(p->name, name);
p->age = age;
return p;
}The Shell and Bash Scripting · Write code · 3 marks
The file words.txt holds one word per line, in no particular order, with repeats. Write a single pipeline that prints the three most frequent words, most frequent first, one word per line and without counts. You may assume there are no ties among the top three. For example, if the file holds
kiwi plum kiwi fig plum kiwi fig pear kiwi plum(one word per line), the pipeline prints kiwi, plum and fig on separate lines. Use any of the standard utilities (sort, uniq, head, awk, cut, sed, tr, …).
sort words.txt | uniq -c | sort -rn | head -n 3 | awk '{print $2}'sort groups equal words, uniq -c counts each group, sort -rn orders by count, largest first, head -n 3 keeps three lines and awk prints only the word. One mark for counting with sort | uniq -c, one for ordering numerically in reverse, one for keeping three and removing the counts.
Compilation, Linking, Headers and Make · Predict the output · 3 marks
The three files below are built with gcc -o prog main.c stats.c. What does the program print?
// stats.h
#ifndef STATS_H
#define STATS_H
static int hits = 0;
void record(void);
int total_hits(void);
#endif
// stats.c
#include "stats.h"
void record(void) {
hits++;
}
int total_hits(void) {
return hits;
}
// main.c
#include <stdio.h>
#include "stats.h"
int main(void) {
record();
record();
record();
printf("%d %d\n", hits, total_hits());
return 0;
}0 3The header is included in two translation units, and the include guard only protects against repeats within one of them. Each translation unit therefore gets its own private static int hits. record and total_hits use the copy in stats.c, which is 3, while main.c prints its own untouched copy, 0.
Software Tools · York University (York, YorkU, York U) · also written EECS2031
Make a free, printable EECS 2031 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 450 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the York library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh EECS 2031 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 York University. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.