Faculty of Computer Studies
M180
Data Structures and Algorithms in Java
Mock Final Exam
Solution
Number of Pages:
(including this cover sheet)
( 10) Time Allowed:
(2 ) Hours
Instructions:
1- Write all your answers on the Answer Booklet.
2- Electronic devices (especially calculators) are not allowed.
Question Type
Max.
Mark
Part 1: Multiple Choice Questions
20
Part 2: Short questions
30
Part 3: Coding questions
50
Total
100
Student Mark
M180
Mock Final-Exam
PART 1: ALL QUESTIONS ARE REQUIRED [20 Marks]
Choose the correct answer:
1) The time complexity of the Merge sort is:
a) O(n2logn)
b) O(n)
c) O(nlog(n))
d) O(n3)
2) The order of “ f= 100n4 –3n +n3 log(n)” is :
a) Θ(n4)
b) Θ(n)
c) Θ(n3logn)
d) Θ(100n4)
3) In Trees, Ancestors of a node are:
a) its children, the children of its children, etc.
b) its parent, the parent of its parent, etc.
c) nodes that have the same parent
d) None of the above
4) In Trees, Interior node is:
a) a node without an edge to another node
b) the origin of the tree
c) connection between nodes
d) a node that is not a leaf node
5) In stacks, Overflow means:
a) trying to pop from an empty stack
b) trying to push onto a full stack
c) trying to push onto an empty stack
d) trying to pop from a full stack
6) A cycle is a path of:
a) length >= 1 that begins and terminates in the same vertex
b) length >= 1 that does not begin and terminate in the same vertex
c) length >= 2 that begins and terminates in the same vertex
d) length >= 2 that does not begin and terminate in the same vertex
2/10
2014-2015
M180
Mock Final-Exam
2014-2015
7) In _____________, divide-and-conquer paradigm is used:
a) Insertion Sort
b) Merge Sort
c) Bubble Sort
d) Selection Sort
8) What does the statement A[A.length-1]= A[1] do?
a) It changes the last element with the second element of the array
b) It changes the last element with the first element of the array
c) It changes the value of the last element of the array using second element
d) It changes the value of the last element of the array using first element
9) A ___________ is a good data structure to use for Top-Down Design:
a) stack
b) queue
c) binary tree
d) list
10) A tree whose every node has maximum 2 children is called a/ an:
a) Binary tree
b) Search Tree
c) N-ary tree
d) None of the above
MCQ-Answers
1
2
3
4
5
6
7
8
9
10
3/10
c
a
b
d
b
a
b
c
c
a
M180
Mock Final-Exam
2014-2015
PART 2: ALL QUESTIONS ARE REQUIRED [30 Marks]
Question 1: (6 marks)
What is the output of the following operations if S is a stack, Q is a queue, and both S and Q are
initially empty?
S.push(5);
S.push(9);
S.push(3);
Q.add(S.pop() + S.pop())
System.Out.println(S.peek())
S.push(4);
S.push(2);
S.push(S.pop() * S.pop())
System.Out.println(S.peek())
Q.add(S.pop() – S.pop())
Q.add(7)
While(!Q.isEmpty())
System.Out.println(Q.remove())
Answer: (1.5 for the first two outputs; others: 1 mark each)
5
8
12
3
7
Question 2: (6 marks)
Fill the table below with the big-O notation and the order (which is a number from 1 to 4,
where 1 represents the asymptotically smallest and 4 represents the asymptotically largest):
Expression
Big-O
Order
100+2n3/10
O(n3)
3
7n3 log n
O(n3logn)
4
50000n2
O(n2)
2
O(1)
700000000
(Each row 1.5 marks: 1 for Big-O, ½ for order)
1
Question 3: (6 marks)
Trace the following method for n=3458
public static int COdd(int n){
if(n==0)
return 0;
else if(n%2!=0)
return 1 + Sum(n/10);
else
4/10
M180
Mock Final-Exam
2014-2015
return sum(n/10)
}
Answer:
COdd(3458)
2
COdd(345)
2+0=2
1+COdd(34)
1+1=2
COdd(3)
1+0=1
1+COdd(0)
1+0=1
0
(3 for decomposition , 3 for composition)
Question 4: (6 marks)
Find the output of the following Java code:
int[][] myArray={{1, 2},{3, 4},{5, 6}};
for(int i=0; i<myArray.length; i++)
{
for(int j=0; j<myArray[i].length; j++)
System.out.print(myArrays[i][j]+" ");
System.out.print(“\n”);
}
Answer: ( 2 marks for each row;deduct 0.5 marks if no space between numbers in a line, or
not starting on a new line after finishing the inner loop)
The output is: 1 2
34
56
5/10
M180
Mock Final-Exam
2014-2015
Question 5: (6 marks)
You are given a Node class and a list class:
public class Node
{
int
data;
Node next;
}
public class list
{
Node first;
}
null
3
Suppose we have a node N as follows:
Suppose we have a linked list L that is initially empty, draw L after each of the following
consecutive operations:
a) L.first=new Node(5, N);
b) L.first.next=new Node(4,N);
c) N.next=L.First;
Answer:(2 marks for each part)
a)
5
First
b)
5
3
null
N
4
First
N
c)
5
First
6/10
null
3
4
3
M180
Mock Final-Exam
2014-2015
PART 3: ALL QUESTIONS ARE REQUIRED [50 marks]
Question 1: (8 marks)
Write the pre-order and in-order traversal of the following binary tree
Pre-order: 15, 5, 3, 12, 10, 6, 7, 13, 16, 20, 18, 23 (4 marks)
In-order: 3, 5, 6, 7, 10, 12, 13, 15, 16, 18, 20, 23 (4 marks)
Question 2: (16 marks)
Consider the following Graph then answer the questions below:
a)
b)
c)
d)
e)
7/10
Is G a directed graph or an undirected? Why? (2 marks)
Give the set of vertices. (2 marks)
Give the set of edges. (6 marks)
Draw the adjacency matrix. (4 marks)
Are there any cycles in the above graph ? If exists, give one. (2 marks)
M180
Mock Final-Exam
2014-2015
Answer:
a) it is directed since there is an edge for example from vertex a to b but not vice versa
(1 mark for correct answer, 1 mark for explanation)
b) The set of vertices is:{a, b, c, d, e, f, g, h} (2 marks; ¼ marks each)
c) The set of edges is:{(a, b), (b, d), (b, e), (b, f), (c, g), (d, a), (d, e), (e, h), (f, c), (f, h), (h, f), (h, g)}
(6 marks; ½ marks each)
d) The adjacency matrix: (4 marks; ½ marks for each line)
A
B
C
D
E
F
G
H
a
0
0
0
1
0
0
0
0
b
1
0
0
0
0
0
0
0
c
0
0
0
0
0
1
0
0
d
0
1
0
0
0
0
0
0
e
0
1
0
1
0
0
0
0
f
0
1
0
0
0
0
0
1
g
0
0
1
0
0
0
0
1
h
0
0
0
0
1
1
0
0
e) Yes (1mark), abd or any correct cycle (1mark)
Question 3: (8 marks)
You are given a Stack class. The following functions are available for use:
public class Stack {
public boolean isEmpty(){};
public void push(int n){};
public int pop(){};
}
Write a method addElements that takes a (Stack S) of integers as input and return the sum
of all the elements in the stack and the stack should finally contain m the same initial
elements. You are only allowed to use Stack object in your method.
For example, if the stack have the following items
2
4
11
8
Then it returns 25
8/10
M180
Mock Final-Exam
2014-2015
Answer: ( using peek() )
public void addElements (Stack S){ // 1 mark
Stack A=new Stack(); // 0.5 marks
int sum=0; // 0.5 marks
while(!S.isEmpty()){ // 1mark
sum =sum + S.peek();// 1.5 marks
A.push(S.pop()) // 1mark
}
while(!A.isEmpty()){// 1mark
S.push(A.pop()) // 1.5 marks
}
}
OR ( without using peek() )
public void addElements (Stack S){ // 1 mark
Stack A=new Stack(); // 0.5 marks
int t,sum=0;
// 0.5 marks
while(!S.isEmpty()){
// 1mark
t=s.pop(); //0.5 mark
sum =sum + t;// 1 marks
A.push(t)
// 1mark
}
while(!A.isEmpty()){// 1mark
S.push(A.pop()) // 1.5 marks
}
}
Question 4: (18 marks)
Use Merge Sort and Bubble Sort to sort the following array:
25
98
45
67
15
45
67
15
Solution:
Merge Sort: ( 10 marks; Divide 5, Conquer 5)
Divide:
25
25
25
25
9/10
98
67
45
45
98
98
98
45
15
15
67
67
15
M180
Mock Final-Exam
2014-2015
Conquer:
Bubble
25
25
25
25
25
25
25
15
Sort: (8 marks)
98
45
67
98
45
67
45
98
67
45
67
98
45
67
15
45
15
67
15
45
67
25
45
67
15
15
15
15
98
98
98
98
End of the Final Exam
10/10
© Copyright 2026 Paperzz