Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

NATIONAL UNIVERSITY OF SINGAPORE

SCHOOL OF COMPUTING
SEMESTER II AY2013/2014

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 FOURTEEN (14) questions. Question 1 TO 12


are MCQ questions.

3. This examination paper comprises ELEVEN (11) printed pages including this front
page. There is an extra blank page at the back.

4. Answer the MCQ questions by shading the OCR form and answer all 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- 12 48
Question 2 30
Question 3 22
Total 100

1
Section A: MCQ ( 12 * 4 Marks = 48 Marks)
1. If str1 = “1231233112233” and str2 = “123”, what is the output of the
following segment of codes?

int count = 0;
for (int j = 0; j < str1.length(); j++) {
if (str1[j] == str2[0]) {
bool match = true;
int k = 1;
while (match && k < str2.length())
if (str1[j+k] == str2[k])
k++;
else
match = false;
if (match) count++;
}
cout << count;
a. 0
b. 2
c. 3
d. 4
e. There is an error.

2. This question refers to the following class and main method:

class NoProblem { int main() {


private: int* _intPtr; NoProblem a(123);
NoProblem b(456);
public: a = b;
NoProblem( int i ) { // point A
_intPtr = new int;
*_intPtr = i; }
}
};

Which of the following statement(s) is/are correct when the execution reaches
Point A?
i. There are memory leakage (i.e. some memory locations are lost to the
program)
ii. The _intPtr pointer in object a and b are pointing to the same
memory location.
iii. The _intPtr pointer in object a and b are pointing to a memory location
with value 123.

A) Only (i)
B) Only (i) and (iii)
C) Only (i) and (ii)
D) Only (ii)
E) (i) , (ii) and (iii)

2
3. Given the following template function:

template <typename T>


T maximum( T left, T right)
{
return (left > right) ? left : right;
}

Suppose we are interested to find out the bigger value between 2.34 and 3.45,
which of the following statement can accomplish this task correctly? You can
assume bigger is a double variable in the following code fragments.

i. bigger = maximum<int> ( 3.45, 2.34 );


ii. bigger = maximum( 3.45, 2.34 );
iii. bigger = maximum<double>( 3.45, 2.34 );

a. (i) only.
b. (i) and (ii) only.
c. (ii) and (iii) only.
d. (i) and (iii) only.
e. (i), (ii) and (iii).

4. Which of the following statement(s) is/are TRUE regarding abstract data type
(ADT)?
i. an ADT can have more than one implementation
ii. an ADT can be used in more than one application
iii. an ADT can have more than one specification

a. (i) only.
b. (i) and (ii) only.
c. (ii) and (iii) only.
d. (i) and (iii) only.
e. (i), (ii) and (iii).

5. Why reference is not same as a pointer?

a. A reference can never be null.


b. A reference once established cannot be changed.
c. Reference doesn't need an explicit dereferencing mechanism.
d. All of the above.
e. None of the above.

6. Which of the following statements is correct?

a. Base class pointer cannot point to derived class object.


b. Derived class pointer cannot point to base class object.
c. Pointer to derived class cannot be created.
d. Pointer to base class cannot be created.
e. None of the above.

3
7. What happens if the base and derived class contains definition of a function with
same prototype?

a. Compiler reports an error on compilation.


b. Only base class function will get called irrespective of object.
c. Only derived class function will get called irrespective of object.
d. Base class object will call base class function and derived class object will call
derived class function
e. None of the above.

8. What is the output of this program?

#include <iostream>
using namespace std;
template<typename T>
class clsTemplate {
public:
T value;
clsTemplate(T i){
this->value = i;
}
void test() {
cout << value << endl;
}
};

class clsChild : public clsTemplate<char> {


public:
clsChild(): clsTemplate<char>( 0 ){ }
clsChild(char c): clsTemplate<char>( c ) { }
void test2(){
test();
}
};

int main(){
clsTemplate <int> a( 42 );
clsChild b( 'A' );
a.test();
b.test();
return 0;
}

a. 42
b. A
c. 42
A
d. A
42
e. None of the above

4
9. What output does this C++ program produce?

#include <iostream>
using namespace std;

class Bird { // in Bird.h


public:
virtual void noise( );
};

void Bird::noise( ) { cout << "chirp "; } // in Bird.cpp

//-----------------------------------------

class Duck: public Bird { // in Duck.h


public:
virtual void noise( );
};

void Duck::noise( ) { cout << "quack "; } // in Duck.cpp

//-----------------------------------------

class Goose: public Bird { // in Goose.h


public:
virtual void noise( );
};

void Goose::noise( ) { cout << "honk "; } //in Goose.cpp

//-----------------------------------------

int main() {
Bird tweety;
Goose ralph;
Duck donald;

tweety = donald;
tweety.noise( );
donald.noise( );
ralph.noise( );

return 0;
}

a. chirp quack honk


b. quack quack honk
c. chirp chirp honk
d. chirp chirp chirp
e. The program won’t compile because the variable types in the assignment
statement aren’t compatible

5
10. The following function reverse() is supposed to reverse a singly linked list. There
is one line missing at the end of the function.

struct ListNode {
int value;
ListNode* next;
};

/* head_ref is a double pointer which points to head


(or start) pointer of linked list */
void reverse(ListNode** head_ref){
ListNode* prev = NULL;
ListNode* current = *head_ref;
ListNode* next;
while (current != NULL){
next = current->next;
current->next = prev;
prev = current;
current = next;
}
/*ADD A STATEMENT HERE*/
}

What should be added in place of "/*ADD A STATEMENT HERE*/", so that the


function correctly reverses a linked list.

a. *head_ref = prev;
b. *head_ref = current;
c. *head_ref = next;
d. *head_ref = NULL;
e. None of the above.

11. Below is a simple function which makes use of the ListNode structure given in
Q10:
ListNode* whatIsThis( ListNode* p, int v ){
ListNode * newP = new ListNode;
newP->value = v;
newP->next = p;
return newP;
}

Suppose we want to use the whatIsThis function to construct a list containing


the values 54321. What is the statement needed to complete the
function?

ListNode* head = NULL;


for (int i = 0; i < 5; i++) {
//fill in your code here
}

a. head = whatIsThis( head->next, i+1 );


b. head = whatIsThis( next, i+1 );
c. head = whatIsThis( head, i+1 );
d. head = whatIsThis( head->next, 5 - i );
e. head = whatIsThis( head, 5 - i );

6
12. The following function takes a single-linked list of integers as a parameter and
rearranges the elements of the list. The function is called with the list containing
the integers 1, 2, 3, 4, 5, 6, 7 in the given order. What will be the contents of the
list after the function completes execution? Note that ListNode is the structure
given in Q10.
ListNode * rearrange(ListNode *list){
ListNode *p, * q;
int temp;
if ((!list) || !list->next)
return list;
p = list;
q = list->next;
while(q){
temp = p->value;
p->value = q->value;
q->value = temp;
p = q->next;
q = p?p->next:0;
}
return list;
}

a. 1,2,3,4,5,6,7
b. 1,3,2,5,4,7,6
c. 2,3,4,5,6,7,1
d. 2,1,4,3,6,5,7
e. None of the above

Section B: Short questions ( 52 Marks)


13. Gambling is a bad habit, it is addictive. It can completely destroy a person and
his/her family. It creates a lot of social problems. Hence it is important that the
exclusion system to ensure that a person is not allowed to enter the local casinos is
implemented successfully.
You are tasked to design the system which takes the following into consideration:
a. The exclusion order can be self-activated or activated by a family member.
b. The person excluded needs to provide his/her figure prints of all ten
figures as a form of identification beside his/her personal ID card number.
c. There should be a system at the entrance of the casinos to check the
identity of people entering the casino and to refuse entry for those persons
under the exclusion order.
d. The government should impose penalty if an excluded person tries to enter
the casinos. A fine of $1000 is imposed for the first offense and double the
previous fine for every subsequent violation.

7
I) Identify at least 5 of the objects involve in the system. Give the name of the
object and a line to briefly describe the object (5 marks)

II) Give specification for 2 object classes of the objects you identified. In each of
these classes, you need to specify the attributes of the objects and two relevant
methods (operations/functions that the object can perform). You only need to
provide the method heading (return type, method name and its parameters) and
not the implementation. But you should use comments to give its purpose, pre-
conditions and post-conditions. Note that the methods should not be
setter/getter methods.

Class 1 (10 marks)

8
Class 2 (10 marks)

III) Use pseudo code to describe the algorithm for imposing the fine for the
offence mentioned. You should indicate the objects involve and how they
communicate with each other. (5 marks)

9
14A. Write a method to insert an integer into a sorted singly linkedlist so that the list
remains sorted after the insertion. You should take care of all the possible cases
such as inserting to an empty list, inserting to the front of the list, inserting to the
end of the list and inserting to other positions in the list. (10 marks)

// pre-cond: the values in the linkedlist are sorted in ascending order


// head is NULL if the list is empty, else it is pointing to the first
// node of the list.
// post-cond: a pointer pointing to the first node of the resultant list is returned
// ListNode is the structure given in Q10 above
ListNode * insertSorted(ListNode * head, int value) {

10
14B. (12 marks)
Card playing is not always bad. Bridge is a game that can help us to exercise our
brain. To help the beginner of the game, you are asked to sort a hand of 13 cards
stored in a Linklist according to the following rules.
i) For the suit, the order is [club, diamond, heart, spade]
ii) For the value, the order is [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], where 11
is ‘J’, 12 is ‘Q’, 13 is ‘K’ and 14 is ‘A’.

Assume that each node in the linklist has the following structure

struct ListNode {
string suit;
int value;
ListNode * next;
};

// pre-cond: A hand of 13 cards is given in the linklist with a head pointer


// post-cond: A sorted list is created and returned
// Notes: You may use the sorting algorithm in C++ STL or write your own
// bruteforce sorting routine to sort the cards. Efficiency is not crucial
// as you only need to sort 13 cards
ListNode * sortingCards(ListNode * head) {

11
- Extra page if needed -

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

12

You might also like