100% found this document useful (1 vote)
148 views14 pages

Final Exam

The document provides instructions for invigilators and students for an examination. It instructs invigilators to distribute the exam booklet and writing paper to students, and to collect only the answer papers at the end. It instructs students to check that their exam paper has 14 pages and 7 questions worth 25 marks each, to choose 4 questions to answer, and to spend 20 minutes reading the questions and 40 minutes on each answer. It says to write answers neatly on the provided paper with name, ID, and question numbers, and that the exam is out of 100 marks and worth 50% of the course grade. It also provides instructions to remain quiet if leaving early.

Uploaded by

abegaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
148 views14 pages

Final Exam

The document provides instructions for invigilators and students for an examination. It instructs invigilators to distribute the exam booklet and writing paper to students, and to collect only the answer papers at the end. It instructs students to check that their exam paper has 14 pages and 7 questions worth 25 marks each, to choose 4 questions to answer, and to spend 20 minutes reading the questions and 40 minutes on each answer. It says to write answers neatly on the provided paper with name, ID, and question numbers, and that the exam is out of 100 marks and worth 50% of the course grade. It also provides instructions to remain quiet if leaving early.

Uploaded by

abegaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

INSTRUCTIONS TO INVIGILATORS:

Give one copy of this examination booklet to each student, and also enough lined
writing paper for answers. Students should not be allowed to bring any notes, books
or handouts into the exam. Collect the answer papers only, checking that the students
have written their name and ID. The students may keep this question paper.

INSTRUCTIONS TO STUDENTS:

Make sure that your exam paper contains 14 pages and 7 questions.

Each question is worth 25 marks. Do not answer all 7 questions. Answer any 4 of
the questions. You should first spend around 20 minutes reading through all of the
questions and deciding which ones you plan to answer. Then aim to spend around 40
minutes on each question.

Write your answers neatly on the separate paper provided, clearly numbering your
answers with the appropriate question numbers. Also write your name and ID number
at the top of each page. You may not use any notes, handouts or textbooks. The
maximum score is 100. Your mark from this examination will make up 50% of your
final mark for this course.

After you have finished, hand your answer sheets only to the invigilator. You may
keep this examination booklet. If you leave the examination early please leave quietly
and try not to disturb other students.

DO NOT OPEN THIS EXAM BOOKLET UNTIL YOU ARE INSTRUCTED


TO DO SO BY YOUR INVIGILATOR

Page 1 of 14
Question 1 - Complexity Analysis (25 marks)

a) Briefly explain the meaning of the following (you do not need to give the formal definition):

i. Ω-notation

ii. θ-notation

(2 marks each – total 4 marks)

b) Explain the difference between big-O and little-O notation.

(2 marks)

c) List, in increasing order of complexity, 4 common complexity classes.

(4 marks)

d) Two algorithms have been developed to solve a particular problem. Algorithm 1 requires 7n2+n
assignment statements, and algorithm 2 requires 109n – 4 assignment statements, where n is the
size of the input data.

i. State the big-O complexities of algorithms 1 and 2.

ii. If it is guaranteed that the size of the input data will never exceed 1 million, which
algorithm is the most efficient?

iii. Which complexity notation, apart from big-O, is appropriate for describing the
complexity of algorithm 2?

(2 marks each - total 6 marks)

e) State the big-O complexity of sequential search in the best, average and worst cases.

(3 marks)

f) For each of the following pieces of code, state and prove the big-O complexity in terms of the
size of the data n (consider assignment statements only). Remember that to prove the
complexity you must find values for the constants c and N that satisfy the formal mathematical
definition.

i. sum = 0;
for (i = 0; i < n; i++)
sum++;

Page 2 of 14
ii. x = 0;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
x += i * j;

iii. for (i = 1; i < n; i *= 2)


sum += i;

(2 marks each - total 6 marks)

Page 3 of 14
Question 2 - Linked Lists (25 marks)

Below is an outline of a C++ class for a singly-linked list of integers. Examine the code and answer
the questions that follow.

//************************ intSLLst.h **************************


// singly-linked list class to store integers

class IntSLLNode {
public:
int info;
IntSLLNode *next;
IntSLLNode(int el, IntSLLNode *ptr = 0) {
info = el; next = ptr;
}
};

class IntSLList {
public:
IntSLList() {
head = tail = 0;
}
~IntSLList();
int isEmpty() {
return head == 0;
}
void addToHead(int);
void addToTail(int);
int deleteFromHead(); // delete the head and return its info;
int deleteFromTail(); // delete the tail and return its info;
void deleteNode(int);
bool isInList(int) const;
void printAll() const;
private:
IntSLLNode *head, *tail;
};

a) Explain, using a diagram, what is meant by a linked list in computer programming. What are the
advantages of linked lists over arrays?

(6 marks)

b) Explain the difference between a singly-linked list and a doubly-linked list. Again, use a
diagram as well as giving a text explanation. What is the advantage of using a doubly-linked
list?

(4 marks)

Page 4 of 14
c) Explain the advantage of including two access pointers (head and tail) for the IntSLList
class.

(2 marks)

d) Write the function body for the addToTail() member function for the IntSLList class.
The function should add a new node at the end of the list.

(5 marks)

e) State the big-O complexity of the addToTail() operation. Justify your answer.

(3 marks)

f) Write a member function to check whether two IntSLList objects have the same contents.
Because the new function is a member function of IntSLList it only needs to take one
argument (another IntSLList object), and return a true or false value. It should return true if
the two lists are identical, and false otherwise.

(5 marks)

Page 5 of 14
Question 3 - Stacks and Queues (25 marks)

a) Briefly describe the operation of the stack and queue data structures. What is the difference
between them? For each, name the five operations that are needed to properly manage the data
structure, and name one programming application in which they would be useful.

(9 marks)

b) What do LIFO and FIFO stand for? Which one describes the operation of a stack and which one
describes a queue?

(4 marks)

c) Show how a stack would appear after each of the operations listed below (i.e. draw a diagram
showing the contents of the stack after each operation):

Push 10, Push 5, Push 8, Pop, Push 1, Pop

(3 marks)

d) Show how a queue would appear after each of the operations listed below (i.e. draw a diagram
showing the contents of the queue after each operation):

Enqueue 3, Enqueue 4, Dequeue, Enqueue 7, Dequeue, Enqueue 2

(3 marks)

e) What is a deque? Describe the operation of a deque, and explain the differences and similarities
between deques and stacks/queues.

(4 marks)

f) Briefly explain the operation of a priority queue.

(2 marks)

Page 6 of 14
Question 4 – Recursion (25 marks)

a) Explain the advantages and disadvantages of using recursion in computer programming. What
factors would you consider when deciding between a recursive and an iterative implementation?

(4 marks)

b) Explain the meaning of the terms tail recursion and nontail recursion. Give one simple example
of each. Which of the two is more easily converted into an iterative equivalent?

(5 marks)

c) What is the purpose of the run-time stack in computer programming? Describe, using a diagram,
the operation of the run-time stack. Clearly indicate what information is typically stored on the
run-time stack, and show how the run-time stack handles recursion.

(8 marks)

d) Briefly explain what would be the output produced by the following recursive function.

void f(int n) {
if (n > 0) {
cout << n << “, “;
f (n – 1);
}
}

(2 marks)

Page 7 of 14
e) The Fibonacci sequence is a series of integers, such that the first and second numbers in the
series are 0 and 1, and any subsequent number is calculated by summing the previous two
numbers in the series. For example, the series starts with 0, 1, 1, 2, 3, 5, 8, 13, etc. The
following is a recursive implementation of a function that computes numbers in the Fibonacci
sequence.

int Fib (int n) {


if (n < 2)
return n;
else
return Fib(n-2) + Fib(n-1);
}

i. Does this function use tail recursion or nontail recursion?

(2 marks)

ii. Do you think recursion is a good choice of implementation in this case? Explain your
answer.

(4 marks)

Page 8 of 14
Question 5 - Trees and Graphs (25 marks)

Examine the trees illustrated below, and answer the questions following.

a) Which of the trees shown above are height-balanced?

(1 mark)

b) Which of the trees shown above are perfectly balanced?

(1 mark)

c) Which of the trees shown above are min heaps?

(1 mark)

d) Which of the trees shown above are max heaps?

(1 mark)

Page 9 of 14
e) Which of the trees shown above are binary search trees?

(1 mark)

f) Write down the order in which the nodes of tree (f) above would be visited in preorder, inorder
and postorder depth-first traversals.

(3 marks)

g) Draw the trees that would result if the 5 node from tree (f) were deleted by the deletion by
merging and deletion by copying techniques.

(4 marks)

h) Indicate how the following max heap would be stored if an array implementation were used (i.e.
draw the array and show what value each array element contains)

(4 marks)

i) Explain the meaning of a rotation in a binary tree. Draw a diagram to show how the positions of
nodes change when a node is rotated. With what type of binary tree is the rotation operation
commonly used?

(4 marks)

Page 10 of 14
Examine the graphs shown below, and answer the questions following.

j) Which of the graphs shown above are directed graphs?

(1 mark)

k) Which of the graphs shown above are weighted graphs?

(1 mark)

l) Which of the graphs shown above are complete graphs?

(1 mark)

m) Which of the graphs shown above are pseudographs?

(1 mark)

n) Which of the graphs shown above are multigraphs?

(1 mark)

Page 11 of 14
Question 6 – Sorting (25 marks)

a) Explain the operation of the selection sort algorithm. Use the following array to illustrate your
answer.

8 3 10 7 1 12 4 15

(4 marks)

b) What is the big-O complexity of selection sort for data movements and comparisons in the best,
average and worst cases?

(3 marks)

c) Programmers developing an application have a requirement to sort a large number of arrays.


Each of these arrays contains less then 10 elements. What sorting algorithm would you
recommend the programmers to use? Explain your answer

(4 marks)

d) Illustrate how the LSD Radix sort algorithm would sort the following list:

100 12 2 101 1 99 192 77

The algorithm should use 3 passes - you should draw pictures showing what the ordering of the
array is after each pass.

(6 marks)

e) What is the big-O complexity of radix sort in the best, average and worst cases? What limitation
of the algorithm makes it sometimes difficult to use?

(2 marks)

f) Describe the operation of the Quicksort algorithm. Use the list given below to illustrate your
answer.

1 4 3 9 6 7 8 5 2

(4 marks)

g) Explain the importance of choosing the right bound in Quicksort. Describe 2 common
techniques for selecting the bound.

(2 marks)

Page 12 of 14
Question 7 – Hashing (25 marks)

a) Hashing aims to reduce the complexity of search operations to O(1). Briefly explain how it can
achieve this. Under what circumstances does it achieve this aim?

(3 marks)

b) When do collisions occur in hashing? What effect do collisions have on the expected search
time for data items in the hash table?

(2 marks)

c) Describe how the following types of hash function work:

i. Mid-square function

ii. Boundary folding

iii. Extraction

(2 marks each – total 6 marks)

d) The following is a sequence of key-value pairs.

Key: 13 5 26 3 14 25 10
Value: A B C D E F G

Describe how these key-value pairs would be added to a hash table of size 10, using a simple
division hash function (i.e. h(K) = K modulo 10), that uses each of the collision resolution
techniques listed below. Show clearly, using a diagram, what the final state of the hash table
would be in each case.

i. Linear probing

ii. Quadratic probing

iii. Bucketing (with a bucket size of 3)

(2 marks each – total 6 marks)

e) Briefly describe the chaining technique in hashing.

(3 marks)

Page 13 of 14
f) Both linear probing and quadratic probing are open addressing techniques. Briefly explain what
open addressing means, and state one problem that can occur with such techniques. Briefly
describe one approach to overcoming this problem.

(5 marks)

END OF EXAMINATION

Examination prepared by: Mekelle University Computer Science Department.

Page 14 of 14

You might also like