0% found this document useful (0 votes)
30 views11 pages

CS1020E Midterm 1213S2

The document describes a midterm exam for a data structures and algorithms course. It contains 10 multiple choice questions testing concepts like pointers, encapsulation, linked lists, and complexity analysis. It also contains 3 longer questions requiring code analysis and implementation of linked list operations.

Uploaded by

Wang Runyu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views11 pages

CS1020E Midterm 1213S2

The document describes a midterm exam for a data structures and algorithms course. It contains 10 multiple choice questions testing concepts like pointers, encapsulation, linked lists, and complexity analysis. It also contains 3 longer questions requiring code analysis and implementation of linked list operations.

Uploaded by

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

NATIONAL UNIVERSITY OF SINGAPORE

SCHOOL OF COMPUTING
SEMESTER II AY2012/2013

CS1020E: DATA STRUCTURES AND ALGORITHMS (For Engineering Students)

Mid Term Test Time Allowed: 1 hour 30 minutes

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.

2. This examination paper consists of THREE (3) questions. Question 1 consists of 10


MCQ questions.

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.

6. This is a closed book examination and you can write in pencil.

EXAMINER’S USE ONLY


Questions Possible Marks Grader Check
MCQ 1- 10 40
Question 2 20
Question 3 17
Total 100

1
MCQ ( 10 * 4 Marks = 40 Marks)
1. What is the output of the program below?

int * mystery(int * arr, int size) {


for (int i=0; i < size; i++)
arr[i] *= 2;
return arr;
}
int main() {
int arr[1] = {1};
int * arr2 = mystery(arr,1);
cout << arr[0] << arr2[0] <<(arr == arr2) << endl;
return 0;
}

a) 111
b) 221
c) 210
d) 120
e) 220

2. What is the output of the following code segment?

void swap(int* a2, int* a1) {


int *temp = a1;
a1 = a2;
a2 = temp;
}

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)

4. Which of the following statement is TRUE regarding encapsulation principle?


a. The main purpose of encapsulation is to increase efficiency of implementation.
b. Encapsulation allows the user of a class to have complete access to the internal
implementation details of a class.
c. Having private methods in a class violates the encapsulation principle.
d. To correctly apply encapsulation principle, we must always provide a full set of
accessor methods and mutator methods for all private attributes.
e. None of the above.

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;
}

a. Remove all the nodes in the even positions.


b. Remove all the nodes in the odd positions.
c. Remove the first duplicate value from the list
d. Remove all the duplicate values from the list
e. None of the above

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;
}

a) Insert the newData as the first node in the linkedlist.


b) Insert the newData as the last node in the linkedlist.
c) Insert the newData as the middle node in the linkedlist.
d) Insert the newData in the sorted position in the linkedlist.
e) None of the above.

4
8. Consider the following program:

template <typename T>


class Pair {
private:
T first;
T second;
public:
Pair (T a, T b): first(a), second(b) { }
T getFirst() {return first;}
T getSecond() {return second;}
void setFirst(T a) {first = a;}
void setSecond(T b) {second = b;}
};

void swap(Pair <int> *pair) {


int temp = pair->getFirst();
pair->setFirst(pair->getSecond());
pair->setSecond(temp);
}

int main ( ) {
int x = 5, y = 6;
// < code to swap x and y >

Which is a correct replacement for <code to swap x and y>?

(i) Pair <int> * myPair = new Pair <int> (x,y);


swap(x, y);
x = myPair.getFirst();
y = myPair.getSecond();

(ii) Pair <int> *myPair = new Pair <int> (x, y);


swap(myPair);
x = myPair->getFirst();
y = myPair->getSecond();

(iii) Pair <int>* myPair = new Pair <int> (x, y);


swap(myPair);
x = myPair->setFirst();
y = myPair->setSecond();

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

listNode * temp = tail;


while (temp != NULL) {
method9(temp); // call the method in question 9
temp = temp->prev;
}
temp = tail;
while (temp != NULL) {
cout << temp->item << “ “;
temp = temp->prev;
}

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.

A) Declare a structure for a treeNode that stores an integer: (5 marks)

struct treeNode {

int _value;
_left; // what is the data type of _left and _right?
_right;

};

Similar to a linked list, we declare a class for BST as follow:


class BST {
private:
treeNode * _root; // pointing to the root node, similar to head
int size; // number of nodes in the BST

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)

// Pre-cond: BST is not empty


// Post-cond: value inserted in BST
void insert(int value) {

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)

// Pre-cond: BST is not empty


// Post-cond: return the number of occurance of the given value,
// return 0 if the value is not in the tree.
int count(int value) {

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.

A) Identify the object classes needed for the system. (5 marks)

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)

~~~~~End of Paper ~~~~

11

You might also like