CS1020E Midterm 1213S2
CS1020E Midterm 1213S2
SCHOOL OF COMPUTING
SEMESTER II AY2012/2013
MATRICULATION NUMBER:
INSTRUCTIONS TO CANDIDATES:
1. Write your matriculation number in the space provided above. Also write your
matriculation number at the top of each sheet in the test paper. Shade your matriculation
number on the OCR form. Remember to sign on the form.
3. This examination paper comprises Eleven (11) printed pages including this front page.
4. Circle the correct option for the MCQ questions and answer all of the other questions
directly in the space given after each question. If necessary, use the back of the page.
5. Marks allocated to each question are indicated. Total marks for the paper is 100.
1
MCQ ( 10 * 4 Marks = 40 Marks)
1. What is the output of the program below?
a) 111
b) 221
c) 210
d) 120
e) 220
int main() {
int A1[2] = {2, 3};
int A2[2] = {4, 5};
swap(A1, A2);
cout<< "[" << A2[0] << ", " << A2[1] << "]" << endl;
}
a. [2, 3]
b. [4, 5]
c. [3, 2]
d. [2, 5]
e. [2, 4]
2
3. A program is to be written that prints an invoice for a small store. A copy of the
invoice will be given to the customer and will display
• A list of items purchased
• The quantity, unit price, and total price for each item.
• The amount due.
Three candidate classes for this program are Invoice, Item, and ItemList, where an
Item is a single item purchased and ItemList is the list of all items purchased. Which
class is a reasonable choice to be responsible for the amountDue method, which
returns the amount the customer must pay?
(i) Item
(ii) ItemList
(iii)Invoice
a. (i) only
b. (iii) only
c. (i) and (ii) only
d. (ii) and (iii) only
e. (i), (ii) and (iii)
5. Given an ADT specification, the different implementations must meet which of the
following requirement(s)?
i. All operations in the specification must be supported with the same
parameters and return type
ii. All operations in the specification must have the same time and space
efficiency
iii. Individual implementation must have different object attributes
a. (i) only.
b. (i) and (ii) only.
c. (ii) and (iii) only.
d. (i) and (iii) only.
e. (i), (ii) and (iii).
3
6. If head is a pointer pointing to a linkedList with the values sorted in descending order,
what is the purpose of the following segment of code?
listNode * current = head;
if (current == NULL) return;
while(current->next!=NULL) {
if (current->data == current->next->data) {
listNode* nextNext = current->next->next;
free(current->next);
current->next = nextNext;
}
else
current = current->next;
}
7. If head is a pointer pointing to a linkedList with the values sorted in descending order,
what is the purpose of the following instance method of the linkedList?
void method8( Data * newData) {
ListNode * Node = new ListNode();
Node->item = newData;
Node->next = NULL;
if (head == NULL) {
head = Node;
return;
}
ListNode * curr = head, *prev = NULL;
while (curr != NULL) {
if (newData > curr->data) {
Node->next = curr;
if (prev == NULL)
head = Node;
else
prev->next = Node;
return;
}
prev = curr;
curr = curr->next;
}
prev->next = Node;
}
4
8. Consider the following program:
int main ( ) {
int x = 5, y = 6;
// < code to swap x and y >
a. (i) only
b. (ii) only
c. (iii) only
d. (ii) and (iii) only
e. None is correct
5
9. Given a doubly linkedlist, what is the purpose of the following function?
// pre-cond: list is not empty and curr is pointing to a node in the list
void method9(listNode * curr) {
listNode * temp1 = curr;
listNode * temp2 = curr->prev;
int item = curr->item;
while (temp2 !=NULL) {
if (temp2->item > item) {
temp1 = temp2;
item = temp2->item;
}
temp2 = temp2->prev;
}
int temp3 = curr->item;
curr->item = item;
temp1->item = temp3;
}
a) Find the smallest item in front of the node pointed to by curr and swap it with the
value in the node pointed to by curr.
b) Find the largest item in front of the node pointed to by curr and swap it with the
value in the node pointed to by curr.
c) Swap the values in the first and last nodes of the linkedlist.
d) Swap the values in the first and second nodes of the linkedlist.
e) None of the above
10. Assume that we have the following doubly linkedlist with a tail pointer pointing to the
last node. What will be the output of the following segment of codes?
tail
4 1 7 6
a) 4176
b) 1467
c) 7641
d) 6714
e) None of the above
6
Short Question ( 52 marks)
Question 2 ( 20 marks)
11. A Binary Search Tree (BST) is an ADT similar to Singly LinkedList where values are
stored in nodes that are pointed to by pointers. The difference is that each node can
have two children (pointed to by left and right pointers) instead of one (pointed to by
a next pointer) as shown below. The nodes at the bottom of the tree are called leaf
nodes and their left and right pointers are NULL. (again similar to the last node in a
Singly LinkedList where next is NULL)
root
14
8 15
8 22
4
5 15 33
The other property of a BST is that all the values on the left subtree is smaller than the
value in the root node and all the values on the right subtree is larger to the value in
the root node as shown in the tree above. When we allow duplicate values to be
present in the BST, the duplicate values are put to the right subtree.
struct treeNode {
int _value;
_left; // what is the data type of _left and _right?
_right;
};
public:
// public methods to be implemented in Part B to D.
};
7
B) Inserting value into BST is done by looking for the correct place in the tree to put
the new value so that the tree is still a BST after the new value is inserted. As such,
new value is always inserted as a leaf node. For example, if you insert the value 3
into the BST above, it would be inserted as the left child of 4. If you insert 15, it
would be inserted as the right child of the node, with value 15, at the bottom of the
tree.
Write the algorithm to insert a value into the BST. Your algorithm should be
detail enough so that someone may convert to C++ method in a 1-to-1 translation
to C++ statement. Of course, you may write your algorithm in C++.
(10 marks)
8
C) Searching on a BST is simple but as allowed to have duplicate values in the tree,
instead of searching for a particular value, we would like to count the occurances
of a given value in the tree. Write the algorithm. (15 marks)
9
D) Deleting from a BST is complicated as there are 3 cases to consider. Here we only
consider deleting a leaf node or a node that have either a left or right child. Write
the algorithm . (15 marks)
// Pre-cond: BST is not empty and the value to be deleted is in the BST
// Post-cond: node containing the value is deleted
void delete(int value) {
10
Question 3 ( 15 marks)
At the moment there is only one set of traffic light within NUS. In view of the possibility
that the population will increase to 6.9 million, NUS administration is enticipating an
increase of traffic flow in campus. Hence to make sure that there is no traffic congestion
in campus, NUS plans to install more traffic lights in some of the junctions. You are
tasked to design the system which includes the set of traffic lights, sensers on the ground
to detect traffic flow and CCTV above ground to monitor the approaching traffic.
B) For two of the classes given in (A), write down the object attributes and at least
two instance methods for each class. Getter and Setter methods will not be
counted. You should also write the purposes of the methods and their pre and post
conditions. (10 marks)
11