Sample CSC209H1 questions
  1. C Basics: Types, Control and Functions · Trace the code · 2 marks

    Trace the loop below. For each iteration, write the values of n and sum at the end of the iteration.

    int n = 3907;
    int sum = 0;
    int steps = 0;
    while (n > 0) {
        sum += n % 10;
        n /= 10;
        steps++;
    }
    Iterationnsum
    1      
    2      
    3      
    4      

    When the loop ends, steps is    .

    Show solution

    Answer: 390; 7; 39; 7; 3; 16; 0; 19; 4

    The loop peels off the last decimal digit of n each time and adds it to sum, so it computes the digit sum 3+9+0+7=193 + 9 + 0 + 7 = 19. After four iterations n reaches 0 and the loop ends with steps equal to 4, the number of digits.

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

  3. File I/O and the Standard Library · Multiple choice · 2 marks

    The file log.txt contains exactly the six bytes abcdef. Then this code runs and no call fails.

    FILE *f = fopen("log.txt", "a");
    fseek(f, 0, SEEK_SET);
    fputs("XY", f);
    fclose(f);

    What does log.txt contain now?

    1. XYcdef

    2. XYabcdef

    3. abcdefXY

    4. Undefined: a stream opened for appending cannot be repositioned.

    Show solution

    Answer: (C)

    (C) In append mode every write goes to the current end of the file, whatever fseek did to the read position. The fseek call is legal but has no effect on where fputs writes. Mode "w" or "r+" would be needed to overwrite the start.

  4. The Shell and Bash Scripting · Short answer · 2 marks

    A student runs sort data.txt > data.txt hoping to sort the file in place. Afterwards data.txt is empty.

    1. Why does this happen? [1]

    2. Write one command line that sorts data.txt in place correctly. [1]

    Show solution
    1. The shell sets up the redirection > data.txt before it starts sort, and opening a file for output truncates it, so sort finds an empty file to read.

    2. sort -o data.txt data.txt, or sort data.txt > tmp && mv tmp data.txt.

  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 CSC209H1 practice exams

CSC209H1 practice exams

Software Tools and Systems Programming · University of Toronto (UofT, U of T, UToronto) · also written CSC209, CSC 209

Make a free, printable CSC209H1 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 CSC209H1 past exams?

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

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