C Basics: Types, Control and Functions · Multiple choice · 2 marks
What happens when this loop runs? (Assume a 32-bit unsigned int.)
for (unsigned int i = 3; i >= 0; i--) {
printf("%u ", i);
}It prints 3 2 1 and stops.
It prints 3 2 1 0 and stops.
The program does not compile because i is unsigned.
It never stops: after 0 the value of i wraps around to 4294967295.
Answer: (D)
An unsigned value is never negative, so i >= 0 is always true. After the iteration with i == 0, the decrement wraps to UINT_MAX (unsigned arithmetic is defined modulo ) and the loop keeps going. To count down to 0 with an unsigned variable, test i > 0 and use i - 1 in the body, or loop while (i-- > 0).
The Stack, the Heap and Dynamic Memory · Short answer · 2 marks
The function below leaks memory on exactly one path. Name the path and give the one-line change that fixes it.
int load(const char *path) {
char *buf = malloc(1024);
if (buf == NULL) {
return -1;
}
FILE *f = fopen(path, "r");
if (f == NULL) {
return -1;
}
size_t n = fread(buf, 1, 1024, f);
fclose(f);
free(buf);
return (int)n;
}When fopen fails, the second return -1 leaves while buf is still allocated and nothing points to it afterwards. Add free(buf); before that return. (The first return -1 is fine because buf is NULL there.)
File I/O and the Standard Library · Multiple choice · 2 marks
A program reads from a descriptor fd that is the read end of a pipe, with
ssize_t n = read(fd, buf, 1000);The call returns 600. Which conclusion is justified?
The end of the input has been reached.
600 bytes are in buf; the program must call read again to get the rest.
An error occurred after 600 bytes; errno says which error.
The other 400 bytes were discarded because buf was too small.
Answer: (B)
(B) A positive return value only says how many bytes were delivered; it may be less than the count requested because only that much was available at the moment. End of input is signalled by a return of 0 and an error by . Nothing is discarded: the next read continues where this one stopped.
The Shell and Bash Scripting · Predict the output · 2 marks
set -- replaces the positional parameters $1, $2, … with its arguments, and shift discards $1 and moves the rest down by one. What does this script print?
set -- red green blue
echo "$#: $1"
shift
echo "$#: $1 $2"
for c in "$@"; do printf '%s,' "$c"; done; echo
set -- "a b" c
echo $#3: red
2: green blue
green,blue,
2After the first shift the parameters are green and blue, so $# is 2. The last set -- gives two parameters because a b is quoted.
Pipes, Redirection and Sockets · Predict the output · 2 marks
Standard output is the terminal, and every call succeeds. What does the program print to the terminal?
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(void) {
int saved = dup(1);
int fd = open("log.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
dup2(fd, 1);
close(fd);
printf("one\n");
printf("uno\n");
fflush(stdout);
dup2(saved, 1);
close(saved);
printf("two\n");
FILE *f = fopen("log.txt", "r");
char line[16];
while (fgets(line, sizeof line, f) != NULL) {
printf("log: %s", line);
}
fclose(f);
return 0;
}two
log: one
log: unoWhile descriptor 1 refers to log.txt, the first two printf lines (flushed before the restore) go into the file, not to the terminal. dup2(saved, 1) points descriptor 1 back at the terminal, so two and then the lines read back from the file are printed there.
Introduction to Software Systems · McGill University (McGill, McGill U) · also written COMP206
Make a free, printable COMP 206 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 550 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the McGill library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh COMP 206 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 McGill University. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.