C Basics: Types, Control and Functions · Short answer · 2 marks
The file hello.c contains only the following code. It is compiled with gcc -std=c17 hello.c and run with ./a.out; echo $?.
#include <stdio.h>
int main(void) {
printf("hi\n");
}What number does the shell print after hi, and why? [1]
If an ordinary function int helper(void) ended the same way, with no return, and the caller used its result, what would the C standard say about the behaviour? [1]
It prints 0. Since C99, reaching the closing brace of main is equivalent to return 0;, so the program reports success.
The behaviour is undefined: this special rule applies only to main, so using the value of a function that fell off its end is undefined behaviour.
The Stack, the Heap and Dynamic Memory · Write code · 2 marks
A program builds a list of names as a malloced array of n pointers, each pointing to its own malloced string. Write free_all, which releases everything.
// free_all(v, n) frees all n strings in v and then the array v itself.
// requires: v and each of v[0..n-1] were returned by malloc
// effects: frees memory
void free_all(char **v, size_t n);void free_all(char **v, size_t n) {
for (size_t i = 0; i < n; i++) {
free(v[i]);
}
free(v);
}The strings must be freed before the array: once v is freed, v[i] may not be read. Freeing only v leaks every string.
Compilation, Linking, Headers and Make · Predict the output · 2 marks
The file e.c contains the lines below. Write the three lines of text that the preprocessor produces (gcc -E -P e.c, ignoring blank lines) and passes to the compiler proper.
#define LIMIT 10
#define NEXT(i) ((i) + 1)
int a = LIMIT;
int b = NEXT(LIMIT);
char *s = "LIMIT";int a = 10;
int b = ((10) + 1);
char *s = "LIMIT";The #define lines are consumed. NEXT(LIMIT) is replaced by its body with the argument LIMIT substituted and then rescanned, giving ((10) + 1). The macro name inside the string literal is left alone.
Pipes, Redirection and Sockets · Multiple choice · 2 marks
A client calls write(fd, msg, 1000) once on a connected TCP socket, where msg holds 1000 bytes. The server, with the connection still open, then calls n = read(cfd, buf, 1000) once. Which statement about n is guaranteed?
n is 1000, because the client sent the bytes in a single call.
n is 0 until the client closes the connection.
n is at least 1 and at most 1000, so the server may need to call read again.
n is either 1000 or -1, because TCP delivers whole messages.
Answer: (C)
TCP is a byte stream with no message boundaries: one write may arrive in several pieces, and several writes may be combined. read blocks until at least one byte is available and then returns whatever is there, up to the size requested. It returns 0 only at end of file, and -1 only on an error.
Software Engineering Practice and Experience: Development Basics · McMaster University (McMaster, Mac, MacU) · also written SFWRENG2XC3
Make a free, printable SFWRENG 2XC3 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.
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 SFWRENG 2XC3 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 McMaster University. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.