The Shell and Bash Scripting · Find the bug · 3 marks
The script cleantmp.sh is meant to delete every file in the current directory whose name ends in .tmp and to print removed NAME for each. It misbehaves in two situations: if a file is named my notes.tmp, and if the directory has no .tmp file at all (it then prints an error message from ls, which it should not).
#!/bin/sh
for f in $(ls *.tmp); do
rm $f
echo "removed $f"
doneExplain what goes wrong in each of the two situations. [2]
Rewrite the script so that it works in both. [1]
The command substitution $(ls *.tmp) produces the names separated by newlines, and the unquoted result is split at every blank, so my notes.tmp becomes two words, my and notes.tmp; rm $f then tries to remove two files that do not exist. When there is no match, the shell passes the literal *.tmp to ls, which reports that it does not exist.
#!/bin/sh
for f in *.tmp; do
if [ -f "$f" ]; then
rm -- "$f"
echo "removed $f"
fi
doneLooping directly over the glob gives one iteration per file, with each name kept whole; quoting "$f" keeps spaces, the -f test skips the unexpanded pattern when nothing matches, and -- protects names that start with a hyphen.
C Basics: Types, Control and Functions · Multiple choice · 1 mark
The variables a and b are ints and b may be 0. Which condition never divides by zero, whatever the values of a and b?
b != 0 && a / b > 2
a / b > 2 && b != 0
b != 0 || a / b > 2
(b != 0) & (a / b > 2)
Answer: (A)
&& evaluates its right operand only if the left one is true, so b != 0 guards the division. The second condition divides before it tests b. In the third, || evaluates its right operand exactly when b != 0 is false, that is, when b is 0. The bitwise & always evaluates both operands.
Pointers, Arrays and Pointer Arithmetic · Short answer · 2 marks
Compare the two declarations
int *a[3];
int (*b)[3];Say what a and b each are, and give sizeof(a) and sizeof(b) on LP64. What is the distance in bytes between b and b + 1?
a is an array of 3 pointers to int: sizeof(a) is . b is a single pointer to an array of 3 ints: sizeof(b) is 8. Adding 1 to b moves it past one whole 3-int array, 12 bytes. The brackets bind tighter than the star, so the parentheses are what make b a pointer.
The Stack, the Heap and Dynamic Memory · Predict the output · 2 marks
What does this program print?
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *p = malloc(sizeof(int));
*p = 5;
int *q = p;
*q += 3;
printf("%d\n", *p);
q = malloc(sizeof(int));
*q = 1;
*p *= 2;
printf("%d %d\n", *p, *q);
printf("%s\n", p == q ? "same" : "different");
free(p);
free(q);
return 0;
}8
16 1
differentq is an alias of p, so *q += 3 changes the one shared int to 8. Then q is repointed at a second block, so *p *= 2 (giving 16) no longer affects *q, and the two pointers differ.
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.
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.
System-Level Programming · Queen's University (Queen's, Queen's U, QueensU) · also written CISC220
Make a free, printable CISC 220 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 250 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the Queen's library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh CISC 220 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 Queen's University. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.