Sample exam solutions

These solutions are for the practice paper used in the second semester running of the course. You can download it here. However, the current running (16s2) isn't going to be a written exam, so this may not be the exact format of the exam.

Please attempt the questions before checking the solutions, as this will be the most beneficial method of study. It can be easy to see that a solution is correct and in hindsight make the question appear easy. However, it can be hard to work out how to arrive at the solution, and this is what you're trying to improve.

Also note that the following questions contain topics we haven't covered in 16s2:

Also, many of the questions can be checked yourself by running the program (or the one you have written) and verifying that it produces the correct result. You might be better off trying to check your solution with the aid of a computer than simply checking the answer.

If you find any problems or think something could use clarification shoot me an email!

Question 1
1
9
25
49
81
121
Question 2
x = 30, y = 20
Question 3
5!! = 15
6!! = 48
Question 4
Line 7: Remove the semicolon after the for loop
Line 10: Should be `min = j`
Line 14 and 15: Swap lines 14 and 15

Comments

Note that for line 7 removing the semicolon is enough. A loop or conditional statement only needs curly braces if it contains more than a single statement. That being said, adding curly braces to the for loop is also a valid solution (and probably what I would do it it were my code).

Question 5
+
|
+-+
| |
+-+-+
| | |
+-+-+-+
| | | |
+-+-+-+-+
Question 6
nahginrek

Comments

This is a recursive function which effectively prints out a given string in reverse.

Question 7
 5 2 1 3 2

Comments

In the exam you may want to leave a note saying that there is a space before 5.

Here's how the linked list grows after each line. The top rectangle is the node's value, and the bottom represents the node's pointer (with a visual representation of what it points to). Note that the X represents NULL.

a = new(1);
b = insert( new(5), insert( new(2), a ));
c = insert( new(3), b->next );
a = insert( a, c );
Question 8a


Question 8b
The function appends string b to string a. The C standard library equivalent to this is strcat
Question 9
15
15
5
3
14
3

Comments

Isn't this a fun question! The only thing I can really advise here is really keep track of your variables. I draw little spaces for them on a piece of paper, with global and function static variables separate from the rest.

Question 10
FFFFFFFF +----------------+
         |                |
BFFFFFFF +----------------+
         |       l        |
         |----------------|
         |                |
         |                |
         |----------------|
         |   items in a   |
         |- - - - - - - - |
         |       k        |
08000000 +----------------+
         |                |
00000000 +----------------+

Comments

I'll try to provide a nicer picture of this, but the ASCII will do for now! l is a local variable, and so is in stack memory. k is a static variable, and is located in an area beneath the heap, where the items in a reside.

Question 11
int is_magic(int a[4][4]){
    int magic = 1;
    int i, j;
    
    int magicNumber = 0;
    for (i = 0; i < 4; i++){
        magicNumber += a[0][i];
    }
    
    for (i = 0; i < 4; i++){
        int colSum = 0;
        int rowSum = 0;
        for (j = 0; j < 4; j++){
            rowSum += a[i][j];
            colSum += a[j][i];
        }
        
        if (rowSum != magicNumber || colSum != magicNumber){
            magic = 0;
        }
    }
    
    int diagSum1 = 0;
    int diagSum2 = 0;
    for (i = 0; i < 4; i++){
        diagSum1 += a[i][i];
        diagSum2 += a[i][3-i];
    }
    
    if (diagSum1 != magicNumber || diagSum2 != magicNumber){
        magic = 0;
    }
    
    return magic;
}

Comments

There's many different ways to do this. I probably have an advantage of sitting at home in a comfy chair while coming up with a solution. In the exam, as long as it produces the correct result, you're fine. You might want to seperate the checking for rows and columns into 2 different loops, or you could even brute force it and check each combination, e.g.

int magicNumber = a[0][0] + a[0][1] + a[0][2] + a[0][3];
// Work out the values for each of the other rows/columns/diagonals in a similar way.
Question 12a
21.125
Question 12b
101101.011
Question 12c
  i) 107
 ii) 10010101
iii) 11000110

Comments

To work out the two's complement of a number we flip the bits (change all 1s to 0s and vice-versa), and then add 1 to the result.

Question 13a
R1 = 01
R2 = 02
R3 = 09
R4 = 03
Question 13b
R3 = 1B

Comments

This is the hexadecimal value of 27. If you realise that the code is computing powers of 3, you can work this out without stepping through the machine code again.

Question 13c
int n = 2;
int value = 1;
int i;

for (i = 0; i < n; i++){
    value *= 3;
}

Comments

I actually find creating an equivalent C program the hardest part! If you spend some time with it, you can probably develop a method to create an easy conversion.