Faculty of Computer Studies
M180
Data Structures and Algorithms in Java
Final Exam
Solution
Fall – 2014
Date : TBA
Number of Pages:
(including this cover sheet)
( 11) 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
Final-Exam
Fall-2014
PART 1: ALL QUESTIONS ARE REQUIRED [20 Marks]
Choose the correct answer:
1) The time complexity of the Bubble sort is:
a) O(n2)
b) O(n)
c) O(nlog(n))
d) O(n3)
2) If we have two strings s1=”draw” and s2=”flow”, then s1.compareTo(s2) gives:
a) 2
b) 0
c) -2
d) False
3) If the characters 'D', 'C', 'B', 'A' are placed in a queue (‘D’ is placed first, and so on), and then
removed one at a time, in what order will they be removed?
a)
b)
c)
d)
ABCD
ABDC
DCBA
Any of the orders
4) Which Boolean expression indicates whether the numbers in two nodes (p and q) are the
same. Assume that neither p nor q is null.
a)
b)
c)
d)
p == q
p.data == q.data
p.next == q.next
None of the above.
5) If a perfect (complete) binary tree has n leaves and all levels are fully populated, how many
nodes does the tree have in terms of n?
a) 2n
b) 2n-1
c) 2n
d) None of the above.
2/10
M180
Final-Exam
Fall-2014
6) In stacks, Underflow means:
a) trying to pop or peek at 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
7) 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 >= 1 that begins and terminates in the same vertex
d) length >= 2 that does not begin and terminate in the same vertex
8) In _____________, divide-and-conquer paradigm is used:
a) Insertion Sort
b) Merge Sort
c) Bubble Sort
d) Selection Sort
9) One of the following structures is used to represent hierarchical relationship:
a) stack
b) queue
c) binary tree
d) list
10) What is the order of the steps for a postorder traversal of a tree:
a)
b)
c)
d)
3/10
1,2,3
3,1,2
2,3,1
2,1,3.
M180
Final-Exam
Fall-2014
PART 2: ALL QUESTIONS ARE REQUIRED [30 Marks]
Question 1: (6 marks)
Find the output of the following Java code:
public static void main(String[] args){
String str="Toyota";
pattern(str, 0);
}
public static void pattern(String s, int i){
if(i<s.length()){
System.out.println(s.substring(0,i+1));
pattern(s, i+1);
}
}
Answer:
Output:
T
To
Toy
Toyo
Toyot
Toyota
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
n4+3n)n2+logn)
O(n4)
3
2n3 (10+ nlogn)
O(n4logn)
4
300000
O(1)
1
O(n2)
2
1600n2
+nlogn
(Each row 1.5 marks: 1 for Big-O, ½ for order)
Question 3: (6 marks)
a) Trace the following method for n=2614
public static int cEven(int n){
if(n==1 || n==0)
return 0;
else if(n%2!=0)
return cEven(n/10);
else
return 1 +cEven(n/10)
}
b) What does this method do?
4/10
M180
Final-Exam
Fall-2014
Answer:
a)
cEven(2614)
3
1+ cEven(261)
2+1=3
cEven(26)
2+0=2
1+ cEven(2)
1+ cEven(0)
1+1=2
1+0=1
0
(2 for decomposition , 2 for composition)
b) It counts the even elements in a number. (2 marks)
Question 4: (6 marks)
What will be the contents of the Queue (myQueue) and the Stack (myStack) after executing the piece of code
below with the given data in myQueue and myStack?
public void fillQ(Queue myQueue, Stack myStack) {
Stack tmp= new Stack();
tmp.push(myStack.pop());
tmp.push(myQueue.remove());
tmp.push(myStack.pop());
tmp.push(myQueue.remove());
tmp.push(myStack.pop());
while (!tmp.isEmpty())
{
myQueue.add(tmp.pop());
}
5/10
M180
Final-Exam
Fall-2014
}
Answer:
-
myQueue will be filled with numbers from 1 to 5 . (4 marks).
myStack will be empty (2 marks).
Or drawing the stack and the queue .
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
7
Suppose we have a node M 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, M);
b) L.first.next.next=new Node(4,null);
c) M.next=L.First;
Answer:(2 marks for each part)
a)
5
First
b)
5
7
null
M
7
4
null
First
c)
5
First
6/10
7
4
null
M180
Final-Exam
Fall-2014
PART 3: ALL QUESTIONS ARE REQUIRED [50 marks]
Question 1: (10 marks)
Answer the following set of questions using the below binary tree:
a)
b)
c)
d)
e)
f)
What is the height of the tree?
What is the depth of node 16?
Give the post-order traversal of the tree.
Give the set of ancestors of node 7.
Which node has no siblings?
Draw a sub-tree from the above tree with size 3.
Answers:
a) Height of the tree is 3 (1 mark)
b) The depth of node 16 is 2 (1 mark)
c) The post-order traversal: 14, 8, 2, 7, 16, 1, 9, 10, 3, 4 (4 marks; higher portion of the grade for the
first ones)
d) The set of ancestors of node 7 are: 16, 1, 4 (1.5 marks)
e) Node 7 has no siblings (1 mark)
f) A sub-tree of size= 3: ( 1.5 marks)
Or
7/10
M180
Final-Exam
Fall-2014
Question 2: (12 marks)
Given the following graph:
a)
b)
c)
d)
e)
Draw the adjacency matrix. (3 marks)
Is G is directed graph or undirected? Why? (2 marks)
Give the set of vertices. (1.5 marks)
Give the set of edges. (3.5 marks)
Are there any cycles in the above graph? If exists, give one. (2 marks)
Answer:
a) The adjacency matrix: (3 marks; ½ marks for each line)
1
2
3
4
5
6
1
0
1
1
0
0
0
2
0
0
0
1
0
0
3
0
0
0
1
0
0
4
0
0
0
0
1
0
5
0
0
0
0
0
1
6
0
0
0
0
0
1
b) 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)
c) The set of vertices is:{1, 2, 3, 4, 5, 6} ( 1.5 marks; ¼ marks each)
d) The set of edges is:{(1, 2), (1, 3), (2, 4), (3, 4), (4, 5), (5, 6),(6,6)}
(3.5 marks; ½ marks each)
e) Yes (1mark), 66 (1mark)
8/10
M180
Final-Exam
Fall-2014
Question 3: (10 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 addEvenElements that takes a (Stack S1) of integers as input and return the
sum of all the even elements in the stack and the stack should finally contains the initial
elements of the stack S1. You are only allowed to use the available Stack object in your
method (not allowed to use peek).
For example, if the stack have the following items:
6
8
13
4
Then it returns 18 , the sum of the even numbers.
Answer:
public int addEvenElements (Stack S1){ // 1 mark
Stack S2=new Stack(); // 0.5 marks
int t,sum=0;
// 0.5 marks
while(!S1.isEmpty()){
// 1mark
t=S1.pop(); //1 mark
if (t%2==0) // 1.5 marks
sum =sum + t;// 1 mark
S2.push(t)
// 1mark
}
while(!S2.isEmpty()){// 1mark
S1.push(S2.pop()) // 1mark
}
return sum;//0.5 marks
}
9/10
M180
Final-Exam
Fall-2014
Question 4: (18 marks)
A) Use Bubble Sort and Insertion Sort to sort the following list of numbers:
7, 5, 2, 4, 3, 9
B) How many swaps were performed in each method?
Answer:
Bubble sort: (9 marks :1 mark per line)
7, 5, 2, 4, 3, 9
5, 7, 2, 4, 3, 9
5, 2, 7, 4, 3, 9
5, 2, 4, 7, 3, 9
5, 2, 4, 3, 7, 9
2, 5, 4, 3, 7, 9
2, 4, 5, 3, 7, 9
2, 4, 3, 5, 7, 9
2, 3, 4, 5, 7, 9
(8 swaps)
Insertion sort: ( 6 marks: 1 mark per line)
7, 5, 2, 4, 3, 9
5, 7, 2, 4, 3, 9
2, 5, 7, 4, 3, 9
2, 4, 5, 7, 3, 9
2, 3, 4, 5, 7, 9
2, 3, 4, 5, 7, 9
(8 swaps)
b) Both Bubble and Insertion sort use 8 swaps (3 marks: 1.5 marks for each )
End of the Final Exam
10/10
© Copyright 2026 Paperzz