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++;
}| Iteration | n | sum |
| 1 | ||
| 2 | ||
| 3 | ||
| 4 |
When the loop ends, steps is .
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 . After four iterations n reaches 0 and the loop ends with steps equal to 4, the number of digits.
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;
}11 21 31
30The 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.
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?
XYcdef
XYabcdef
abcdefXY
Undefined: a stream opened for appending cannot be repositioned.
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.
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.
Why does this happen? [1]
Write one command line that sorts data.txt in place correctly. [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.
sort -o data.txt data.txt, or sort data.txt > tmp && mv tmp data.txt.
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;
}two
log: one
log: unoWhile 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.
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.
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.
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.