Test 1 2017
Test 1 2017
WESTVILLE CAMPUS
Theory Test I
Examiner: Mr E. Jembere
Marks 50 marks
Duration 1hr 20 mins
Instructions
c. If a linked list has only one node, both the head and tail reference that node.
d. The best-case time complexity for adding n elements in to a heap is O(n Log (n)).
f. When you create an array using ArrayList x = new ArrayList(10), x.size() is 10.
g. The interfaces Set, Map, and List are all sub-interfaces to the collection interface.
1|Page
Thursday, August 24, 2017
i. 𝑓(𝑛) 𝑖𝑠 𝑛𝑂(1) means the function 𝑓(𝑛) is bounded above by a polynomial function.
[10]
2. Suppose that set1 (Set<Object>) is a set that contains the strings "red", "yellow", "green", and that set2
(Set<String>) is another set that contains the strings "red", "yellow", "blue". Answer the following
questions: (All the questions are independent)
a) What is set1 after executing set1.addAll(set2)?
[5]
3. The program in Figure 1 creates a map with an ID number and Full name as a key/value pair for each
entry. Show the output of the code in Figure 1.
Figure 1
[5]
4. Show that the worst case time complexity for the quick sort algorithm is O(𝑛2 ).
[6]
2|Page
Thursday, August 24, 2017
𝐢𝐧𝐭 𝐜𝐨𝐮𝐧𝐭 = 𝟎;
𝐟𝐨𝐫(𝐢𝐭 𝐢 = 𝟏; 𝐢 <= 𝐧; 𝐢 ∗= 𝟒 )
𝐟𝐨𝐫(𝐢𝐧𝐭 𝐣 = 𝟏; 𝐣 <= 𝐢; 𝐣 + + )
𝐜𝐨𝐮𝐧𝐭 + +;
Figure 2
[6]
6. The study the code snippet Figure 3 and answer the following questions:
Figure 3
b. Which special case of deleting a node is being handled in the else if block in lines 4 – 9.
d. The program in Figure 4 runs in O (n) time. Rewrite the code such that it runs in constant time,
0(1).
3|Page
Thursday, August 24, 2017
1 public E removeNode() {
2 if (tail == null)
3 return null;
4 else if (head==tail) {
5 DLLNode<E> temp = head;
6 head = tail = null;
7 size --;
8 return temp.element;
9 }
10 else {
11 DLLNode<E> current = head;
12 for (int i = 0; i < size - 2; i++)
13 current = current.next;
14 DLLNode<E> temp = tail;
15 tail = current;
16 tail.next = null;
17 size--;
18 return temp.element;
19 }
20 }
Figure 4
[2, 1, 2, 2]
8. Using the definition of a node given in Figure 5, write the java code for an implementation of the
following methods in a circular-doubly linked list.
Figure 5
4|Page