Schedule
Labs
Assignments
TA office hours
Topic videos
Some course notes
Extra problems
Lecture recordings
Total: 30 marks in five questions.
Time allotted: 45 minutes.
Since time is short, be careful not to get stuck on one question to the exclusion of others. Not everyone will necessarily be able to finish this test within the 45 minutes.
The amount of marks or answer-space allotted does not indicate how long it will take you to complete the question, nor does the size of the answer-space indicate the size of the correct answer.
Answer all questions. Answer questions in the space provided.
Be sure to write backquotes and single-quotes correctly and distinctly. We can only grade what you wrote, not what you meant.
Do not open this booklet until you are instructed to.
1. [3 marks] Write an 'echo' command which produces the following output:
I'm *so* happy to be "here"!with all punctuation appearing on the output exactly as shown — single quote (apostrophe), asterisks, double-quotes.
2. [5 marks] Write a complete C program which processes data from its standard input to its standard output, swapping the characters 'x' and 'y' — any 'x' becomes 'y' and any 'y' becomes 'x', just like a "tr xy yx". For example, "syntax" would turn into "sxntay". All other characters are copied from stdin to stdout untransformed (including newlines, for example). (This program does not declare the argc and argv parameters to main().)
#include <stdio.h> int main() { int c; while ((c = getchar()) != EOF) { if (c == 'x') putchar('y'); else if (c == 'y') putchar('x'); else putchar(c); } return(0); }
3. [7 marks] What is the complete output from each of these sets of shell commands?
Solutions note: You can simply try them. Then you can try variations, if you don't understand why they output what they do.
a)
x=34 echo x expr $x + 3
Actual output (solution):
x 37
b)
for i in `seq 5 8` do echo $i done
Actual output (solution):
5 6 7 8(no penalty in grading for omitting the 8)
c)
for i in seq 5 8 do echo $i done
Actual output (solution):
seq 5 8
4. [5 marks] Write a function in C (not a complete program) which takes two parameters: an array of ints, and the size of that array. Your function will find the maximum distance between any two adjacent elements (as in a shell programming problem in assignment one) and return this integer. (Unlike the shell programming problem in assignment one, it does not keep track of which two elements had this maximum distance.)
Additional advice which should have been in the original question (apologies): If the array contains only zero or one items, return −1. (Of course, this can't now be insisted upon in grading.)
int dist(int *p, int size) { if (size < 2) return(-1); int maxdist = -1, newdist; for (int i = 1; i < size; i++) { newdist = p[i-1] - p[i]; if (newdist < 0) newdist = -newdist; if (newdist > maxdist) maxdist = newdist; } return(maxdist); }Given the omission in the question, what your code returns if size<2 will not be graded, although if your code exceeds array bounds in this case (e.g. if it accesses p[−1]), that will still be penalized (that's an issue about C, not about this test question).
5. [5 marks] The following declarations are in place:
int i; int *p; int x[100];Furthermore, i has been assigned to be 3; and p has been assigned to point to x[2]. For each of these assignment statements, write a possible declaration for the variable on the left side of the equals sign which would make this assignment statement valid.
Example:
Question: a = 12;
Possible answer: int a;
Questions for you:
b = '@';
Sample solution: int b;
c = &i;
Sample solution: int *c;
d = p;
Sample solution: int *d;
e = &p;
Sample solution: int **e;
f = p + i;
Sample solution: int *f;
g = x[i];
Sample solution: int g;
h[100] = &p;
Sample solution: int **h[101];
Another sample solution: int ***h;