Sample COMPSCI 1XC3 questions
  1. C Basics: Types, Control and Functions · Short answer · 3 marks

    The function below should print the n elements of a from last to first. Here size_t is an unsigned 64-bit type.

    void print_backwards(const int *a, size_t n) {
        for (size_t i = n - 1; i >= 0; i--) {
            printf("%d\n", a[i]);
        }
    }
    1. Give two different ways this loop goes wrong. [2]

    2. Rewrite the loop so that it is correct for every n, including 0, and still uses a size_t counter. [1]

    Show solution
    1. (1) The counter is unsigned, so i >= 0 is always true: after a[0] is printed, i-- wraps around to SIZE_MAX and the loop reads far outside the array. (2) If n is 0, then n - 1 already wraps to SIZE_MAX and the very first iteration reads out of bounds.

    2. Count down from n to 1 and subtract one when indexing:

      for (size_t i = n; i > 0; i--) {
          printf("%d\n", a[i - 1]);
      }
  2. The Stack, the Heap and Dynamic Memory · Multiple choice · 2 marks

    After the following code runs, which statement is true?

    char *p = malloc(8);
    char *q = p;
    free(p);
    1. q still holds the old address, but reading or writing through it is undefined.

    2. q was set to NULL because it pointed to the block that was freed.

    3. free(q) is now safe, because q is a different variable from p.

    4. q still refers to 8 valid bytes until the program reuses p.

    Show solution

    Answer: (A)

    p and q are two pointers to the same block, and the block is gone once either is freed. q keeps the stale address (a dangling pointer): dereferencing it is undefined behaviour, and free(q) would be a double free. No pointer is changed automatically, and reusing p has nothing to do with it.

  3. 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;
    }
    1. Identify the two bugs by line number and say what goes wrong at run time. [2]

    2. Rewrite the function correctly. [1]

    Show solution
    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.

    2. 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;
      }
  4. Compilation, Linking, Headers and Make · Short answer · 3 marks

    Two students build a program in which geom.c defines int area(int w, int h).

    • Student 1’s main.c calls area(3, 4) but has neither a prototype for area nor an #include that provides one. The compiler warns about an implicit declaration of area (a recent compiler rejects the file).

    • Student 2’s main.c has the correct prototype, but the command is gcc -o prog main.c. The build fails with an undefined reference to area.

    For each student, name the stage of the build that reports the problem and say what is missing.

    Show solution

    Student 1: the compilation stage reports it. The compiler is missing a declaration of area, so it does not know its parameter and return types; the fix is a prototype, usually through a header such as geom.h.

    Student 2: the linking stage reports it. The call compiled because the declaration is present, but no object file or library on the command line provides a definition of area; the fix is to add geom.c (or geom.o) to the command.

  5. Pipes, Redirection and Sockets · Find the bug · 2 marks

    The function echo should send back to the client everything that the client sends, until the client closes the connection. It has two bugs.

    void echo(int fd) {
        char buf[100];
        ssize_t n;
        while ((n = read(fd, buf, sizeof buf)) != 0) {
            write(fd, buf, sizeof buf);
        }
        close(fd);
    }
    1. Identify the two bugs by line number and say what goes wrong. [1]

    2. Rewrite the loop (lines 4 to 6) correctly. [1]

    Show solution
    1. Line 5: write sends sizeof buf (100) bytes instead of the n bytes just read, so the client receives stale or uninitialised bytes after the real data. Line 4: the loop condition is != 0, so a read error (-1) keeps the loop running, normally forever, instead of ending it.

    2. while ((n = read(fd, buf, sizeof buf)) > 0) {
          write(fd, buf, (size_t)n);
      }
About COMPSCI 1XC3 practice exams

COMPSCI 1XC3 practice exams

Computer Science Practice and Experience: Development Basics · McMaster University (McMaster, Mac, MacU) · also written COMPSCI1XC3

Make a free, printable COMPSCI 1XC3 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.

Looking for COMPSCI 1XC3 past exams?

Start with the past exams and tests your instructor or the McMaster library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh COMPSCI 1XC3 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
  • Pipes, Redirection and Sockets · 50 questions

More McMaster courses

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