Sample CPS 125 questions
  1. C Basics: Types, Control and Functions · Write code · 3 marks

    Write the function count_digits. It must work for every int, including INT_MIN, so it must not negate n.

    // count_digits(n) returns the number of decimal digits of |n|.
    // The number 0 has one digit.
    int count_digits(int n);

    For example, count_digits(0) is 1, count_digits(-123) is 3 and count_digits(1000) is 4.

    Show solution
    int count_digits(int n) {
        int digits = 0;
        do {
            digits++;
            n /= 10;
        } while (n != 0);
        return digits;
    }

    A do-while loop runs the body once before testing, which makes 0 count as one digit without a special case. Division truncates toward zero, so n /= 10 works for negative numbers too and never overflows. One mark for the loop, one for handling 0, one for handling negative numbers.

  2. Pointers, Arrays and Pointer Arithmetic · Write code · 3 marks

    Write the function below. Use no library functions. Take care that n may be 0 and may be odd.

    // reverse_ints(a, n) reverses the order of a[0..n-1] in place.
    //   For example {1, 2, 3, 4} becomes {4, 3, 2, 1}.
    // requires: a is valid for n elements (n may be 0)
    // effects: mutates a
    void reverse_ints(int *a, size_t n);
    Show solution
    void reverse_ints(int *a, size_t n) {
        int *lo = a;
        int *hi = a + n;
        while (lo < hi) {
            hi--;
            int t = *lo;
            *lo = *hi;
            *hi = t;
            lo++;
        }
    }

    Start hi one past the end and decrement it before using it, so that a + n - 1 is never formed when n == 0. When the pointers meet (odd n) the middle element swaps with itself and the loop ends. Marks: setup (1), loop condition (1), correct swap (1).

  3. C Strings and Buffers · Predict the output · 2 marks

    What does this program print?

    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
        char buf[16];
        strcpy(buf, "ab");
        strcat(buf, "cd");
        strcpy(buf + 1, "xyz");
        printf("%s %zu\n", buf, strlen(buf));
        buf[3] = '\0';
        strcat(buf, "!");
        printf("%s\n", buf);
        return 0;
    }
    Show solution
    axyz 4
    axy!

    After the first two calls buf is "abcd". Copying "xyz" to buf + 1 overwrites everything from index 1, including the old terminator, giving "axyz". Putting '\0' at index 3 cuts the string to "axy", and strcat appends after it.

  4. The Stack, the Heap and Dynamic Memory · Predict the output · 2 marks

    What does this program print?

    #include <stdio.h>
    
    int next(void) {
        static int calls = 0;
        int fresh = 0;
    
        calls++;
        fresh++;
        return calls * 10 + fresh;
    }
    
    int main(void) {
        int a = next();
        int b = next();
        int c = next();
        printf("%d %d %d\n", a, b, c);
        printf("%d\n", next() - a);
        return 0;
    }
    Show solution
    11 21 31
    30

    The static variable calls survives between calls (1, 2, 3, 4) while fresh is created afresh as 0 each time and is always 1 when returned. The fourth call returns 41, and 41 - 11 is 30.

  5. File I/O and the Standard Library · Predict the output · 2 marks

    What does this program print? Assume that int is 4 bytes and that every call succeeds.

    #include <stdio.h>
    
    int main(void) {
        int a[4] = {5, 6, 7, 8};
        FILE *f = fopen("ints.bin", "wb");
        fwrite(a, sizeof(int), 4, f);
        fclose(f);
    
        f = fopen("ints.bin", "rb");
        fseek(f, 0, SEEK_END);
        printf("%ld\n", ftell(f));
        fseek(f, 2 * sizeof(int), SEEK_SET);
        int x;
        size_t got = fread(&x, sizeof(int), 1, f);
        printf("%d %zu\n", x, got);
        int b[2];
        got = fread(b, sizeof(int), 2, f);
        printf("%d %zu\n", b[0], got);
        fclose(f);
        return 0;
    }
    Show solution
    16
    7 1
    8 1

    The file holds 4 ints of 4 bytes, so ftell at the end is 16. Seeking to byte 8 and reading one int gives a[2], 7. Only one int (8) remains, so the next fread of two items delivers one and returns 1.

About CPS 125 practice exams

CPS 125 practice exams

Digital Computation and Programming · Toronto Metropolitan University (TMU, Ryerson) · also written CPS125

Make a free, printable CPS 125 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 300 original questions and skips the ones you have already seen.

Looking for CPS 125 past exams?

Start with the past exams and tests your instructor or the TMU library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh CPS 125 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
  • C Strings and Buffers · 50 questions
  • The Stack, the Heap and Dynamic Memory · 50 questions
  • Structs and Linked Structures · 50 questions
  • File I/O and the Standard Library · 50 questions

More TMU courses

questionbank.dev is independent and not affiliated with Toronto Metropolitan University. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.