Sample COMP 206 questions
  1. 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);
    }
    1. It prints 3 2 1 and stops.

    2. It prints 3 2 1 0 and stops.

    3. The program does not compile because i is unsigned.

    4. It never stops: after 0 the value of i wraps around to 4294967295.

    Show solution

    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 2322^{32}) 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).

  2. 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;
    }
    Show solution

    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.)

  3. 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?

    1. The end of the input has been reached.

    2. 600 bytes are in buf; the program must call read again to get the rest.

    3. An error occurred after 600 bytes; errno says which error.

    4. The other 400 bytes were discarded because buf was too small.

    Show solution

    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 −1-1. Nothing is discarded: the next read continues where this one stopped.

  4. 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 $#
    Show solution
    3: red
    2: green blue
    green,blue,
    2

    After the first shift the parameters are green and blue, so $# is 2. The last set -- gives two parameters because a b is quoted.

  5. 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;
    }
    Show solution
    two
    log: one
    log: uno

    While 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.

About COMP 206 practice exams

COMP 206 practice exams

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.

Looking for COMP 206 past exams?

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.

Topics covered

  • C Basics: Types, Control and Functions · 50 questions
  • Pointers, Arrays and Pointer Arithmetic · 50 questions
  • The Stack, the Heap and Dynamic Memory · 50 questions
  • C Strings and Buffers · 50 questions
  • Structs and Linked Structures · 50 questions
  • File I/O and the Standard Library · 50 questions
  • Compilation, Linking, Headers and Make · 50 questions
  • The Shell and Bash Scripting · 50 questions
  • Processes: fork, exec and wait · 50 questions
  • Signals · 50 questions
  • Pipes, Redirection and Sockets · 50 questions

More McGill courses

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.