-2-
1.
(a)
Consider the following code fragment:
int f(int n, int m) {
int k = 0;
3.
while (n != m) {
4.
n = n – m;
5.
if (n > 0) {
6.
k = k + 1;
7.
} else {
8.
n = m;
9.
}
10.
}
11.
return k;
12. }
1.
2.
Find the final return value, given the following input values. Show
your working.
(i) n = 9, m = 3
[3 marks]
(ii) n = 15, m = 2
[3 marks]
(iii) n = 2, m = 7
[3 marks]
(iv) n = 4, m = 4
[3 marks]
(b) Suppose that a programmer stores a sequence of integers {0, 2,
4, 6, 8, 10} in a queue using enqueue operations – e.g.,
q.enqueue(0), q.enqueue(2),..., q.enqueue(10).
(i)
What is the value returned if we perform a q.dequeue()
operation? Explain your answer.
[2 marks]
(ii)
How many q.dequeue() operations are needed to retrieve
the value 4 of the sequence?
[2 marks]
(iii) Consider the following code fragment.
1.
2.
3.
4.
5.
6.
7.
Queue<Integer> q2 = new Queue<Integer>();
while (!q.isEmpty()) {
q2.enqueue(q.dequeue());
}
while (!q2.isEmpty()) {
System.out.println(q2.dequeue() + “ “);
}
What is the output that is printed?
[4 marks]
-3-
2.
(a)
Suppose that a sequence of integer values from 0 to 1,000,000
is stored in an array and a linked list. For each of the statements
regarding computational complexity below, say whether it is true of
false, and briefly justify your answer.
(i)
If we want to print the values at positions multiple of 5 (e.g.,
5th, 10th, 15th, so forth), it is more computationally efficient to
use the array than then linked list.
[2 marks]
(ii)
If we want to remove the first value of the sequence, it is more
computationally efficient to use the array than the linked list.
[2 marks]
(iii) If we want to print all values, it is more computationally
efficient to use the array than the linked list.
[2 marks]
(b) Consider the following pseudocode of a recursive function:
1.
2.
5.
6.
7.
8.
9.
int mult3(int n) {
if (n == 0) {
return 0;
} else {
return 3 + mult3(n – 1);
}
}
(i)
What is the return value of the function for mult3(1)?
[1 mark]
(ii)
What is the return value of the function for mult3(3)?
[1 mark]
(iii) Provide a high-level description of what the function does
when given a positive number.
[2 mark]
(iv) Describe the behaviour of the function when a negative value
is given – e.g., multi3(-1).
[2 mark]
(v)
Rewrite the pseudocode so that it operates as an iterative
function.
[8 marks]
-4-
3.
(a)
(i)
In the following graph, what is the shortest path from node V0
to V3? Give the nodes that you would travel through on this
shortest path.
[2 marks]
(ii)
In the same graph, what is the shortest path from node V3 to
V0? Again, state the nodes travelled through.
[2 marks]
(iii) Is this graph an example of a DAG?
[1 mark]
(b) Looking at the following binary tree, give the output of the following
traversal operations:
(i)
pre-order traversal
[3 marks]
(ii)
in-order traversal
[3 marks]
(iii) post-order traversal
(c)
[3 marks]
For the tree pictured in part (b), give the list of nodes that you look
at if performing the following search operations to search for
“Kemi” in the tree:
(i)
Depth first search
[3 marks]
(ii)
Breadth first search
[3 marks]