Sample CMPUT 201 questions
  1. C Basics: Types, Control and Functions · Short answer · 2 marks

    What does this program print, and why?

    #include <stdio.h>
    
    void swap(int a, int b) {
        int t = a;
        a = b;
        b = t;
    }
    
    int main(void) {
        int x = 3;
        int y = 7;
        swap(x, y);
        printf("%d %d\n", x, y);
        return 0;
    }
    Show solution

    It prints 3 7. C passes arguments by value: a and b are new variables initialised with copies of x and y, and swap exchanges only those copies, which disappear when it returns. To change the caller’s variables, swap would have to receive their addresses.

  2. Pointers, Arrays and Pointer Arithmetic · Complete the code · 2 marks

    Complete my_strlen, which returns the number of characters before the terminating '\0', by filling in the three blanks.

    size_t my_strlen(const char *s) {
        const char *p = ___(1)___;
        while (___(2)___) {
            p++;
        }
        return ___(3)___;
    }
    (1)   
    (2)   
    (3)   
    Show solution

    Answer: s; *p; p - s

    (1) s (2) *p (equivalently *p != '\0') (3) p - s

    size_t my_strlen(const char *s) {
        const char *p = s;
        while (*p) {
            p++;
        }
        return p - s;
    }

    The pointer starts at the first character and stops on the terminator. The difference of two pointers into the same array is the number of elements between them, which is exactly the length.

  3. C Strings and Buffers · Complete the code · 2 marks

    The function append appends src to the string in dst, a buffer of cap bytes, but only if the whole result, terminator included, fits. It returns 1 on success, and 0 (leaving dst unchanged) otherwise. Write the code for A, B and C.

    int append(char *dst, size_t cap, const char *src) {
        size_t used = strlen(dst);
        size_t need = ____A____;
        if (____B____) {
            return 0;
        }
        ____C____;
        return 1;
    }
    Show solution
    A:  used + strlen(src) + 1
    B:  need > cap
    C:  strcpy(dst + used, src)

    The space needed is the current length, the new characters and one byte for the terminator. If that exceeds cap nothing may be written. Otherwise the copy starts at the old terminator. (memcpy(dst + used, src, strlen(src) + 1) is also correct.)

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

    What does this program print? Assume that every call succeeds.

    #include <stdio.h>
    
    int main(void) {
        FILE *f = fopen("data.txt", "w");
        fputs("ab\ncd\n", f);
        fclose(f);
    
        char buf[8];
        f = fopen("data.txt", "r");
        while (fgets(buf, sizeof buf, f) != NULL) {
            printf("[%s]", buf);
        }
        fclose(f);
        printf("\n");
        return 0;
    }
    Show solution
    [ab
    ][cd
    ]

    fgets stores the newline at the end of each line it reads, so each buffer is "ab\n" or "cd\n", and the closing bracket of each pair lands at the start of the next line. The final printf supplies the last newline.

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

About CMPUT 201 practice exams

CMPUT 201 practice exams

Practical Programming Methodology · University of Alberta (UAlberta, UofA, U of A) · also written CMPUT201

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

Looking for CMPUT 201 past exams?

Start with the past exams and tests your instructor or the UAlberta library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh CMPUT 201 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

More UAlberta courses

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