MT1011S2 cg1103 Ans
MT1011S2 cg1103 Ans
MT1011S2 cg1103 Ans
SCHOOL OF COMPUTING
SEMESTER II AY2010/2011
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 TWELVE (12) printed pages including this front
page.
4. Answer the MCQ questions by shading the OCR form and answer all of the other
questions directly in the space given after each question. You are allowed to answer
in PENCIL.
5. Marks allocated to each question are indicated. Total marks for the paper is 100.
6. This is a CLOSE BOOK test. You are allowed to bring in ONE (1) piece of A4
handwritten reference sheet. No photocopies allowed.
1
MCQ ( 10 * 5 Marks = 50 Marks)
temp = ipA;
ipA = ipB;
ipB = temp;
}
int main()
{
int i = 123, j = 456;
2
4. Suppose we have a function with the following header:
void funcOne( SomeClass& obj);
a. (i) only.
b. (i) and (ii) only.
c. (ii) and (iii) only.
d. (i) and (iii) only.
e. (i), (ii) and (iii).
a. 0
0
b. 2
2
c. 4
4
d. 2
4
e. 2
undefined (some random values)
3
6. Using the same template function in Q5, what is the result of the following code
fragment?
int i, j, k;
i = 123;
j = 123;
k = 246;
a. 2
b. 4
c. 5
d. 6
e. address of variable "j" is printed
7. In our course, we use the following statement in order to make use of an ADT:
For example:
ListBase* listPtr = new ListArray;
listPtr->insert( 1, 123 );
Which of the following mechanism(s) is/are essential for the above design /
organization to work properly?
a. (i) only.
b. (i) and (ii) only.
c. (ii) and (iii) only.
d. (i) and (iii) only.
e. (i), (ii) and (iii).
4
8. Given the two different approaches to read in 3 integers below:
Approach A:
int a, b, c;
cin >> a;
cin >> b;
cin >> c;
Approach B:
int a, b, c;
string input;
getline( cin, input );
User Y:
123
<empty line>
456
789
User Z:
123<tab>456<tab>789 //<tab> refers to the tab key
5
10. Below is the Pair template class discussed in lecture:
public:
Pair(T1 a, T2 b) : _first(a), _second(b) {}
a. Alan Turing
b. Ken Thompson and Dennis Ritchie
c. Bjarne Stroustrap
d. James Gosling
e. Uncle Soo
6
Short Question ( 50 marks)
Question 2 ( 24 marks)
head
next
1 1 1 2 1 1
We'd like to "compact" the linked list by removing adjacent nodes with the same
value. For example, the above linked list can be compacted to:
head next
1 2 1
Give a C++ function to perform the compaction as described. Write your code in the
box provided in the next page.
You can assume that each linked list node is declared as follows:
struct ListNode {
int item;
ListNode* next;
};
7
void compact( ListNode* head )
//Pre-condition: None
//Post-condition: Linked List starting from "head" is compacted
// as described in the question
{
cur = head;
if (cur->item == cur->next->item){
temp = cur->next;
cur->next = cur->next->next;
delete temp;
} else {
cur = cur->next;
}
}
//Important checks:
- Handles empty or 1 node list correctly?
- Any segmentation fault? e.g. ptr->item when ptr
may be NULL?
- Is "delete" used?
8
(Part II – 12 marks) Suppose we want to "expand" a given linked list with the
following rules:
For every node N in the linked list, the item stored (which is a positive integer)
is retrieved. Let's call the item X.
o The node N will then be duplicated X times and inserted after node N.
e.g. If a node stores the value "5", the node will be duplicated 5 times.
Note that the nodes duplicated are ignored in subsequent processing.
Below are some examples. The nodes duplicated are shown in bold.
You can assume that the item stored in each linked list node is a positive integer.
cur = head;
while ( cur != NULL ){
times = cur->item;
//Duplicate "times" number of nodes
for (int i = 0; i < times; i++){
newNode = new ListNode;
newNode->item = times;
newNode->next = cur->next;
cur->next = newNode;
//Important checks:
- Handles empty or 1 node list correctly?
- Create and link up new nodes correctly?
- Skip processed nodes correctly?
9
Question 3 ( 10 marks)
Given a string S, and a shorter non-empty string T, we would like to count the number of
occurrences of T in S.
Write a function to perform the above. You are only allowed to use the following predefined
string methods and simple indexing to help:
int length();
//return the number of characters in string
int count = 0;
for(int idx = 0;
idx <= s.length() – t.length(); idx++) {
if (s.substr( idx, t.length()) == t )
count++;
}
return count;
//Important checks:
- Terminate as early as possible?
- Extract and compare correctly?
10
Question 4 ( 16 marks )
class A
{
public:
void M1() { cout << "A.M1\n"; }
virtual void M2() { cout << "A.M2\n"; }
void M3() {
this->M1();
this->M2();
}
};
class B: public A
{
public:
virtual void M2() { cout << "B.M2\n"; }
};
class C: public B
{
public:
void M1() { cout << "C.M1\n"; }
};
For each of the code fragment shown on next page, indicate whether:
The code will cause compilation error. Briefly explain the reason. OR
The code can compiles and execute. Supply the execution result in this case.
Note that any code fragment with both choices filled will be taken as incorrect. Each
code fragment should be considered on its own, independent from other code
fragments. See below for two simple examples.
11
Code Fragment Compilation Error Execution Result
A* a = new B; A.M1
a->M1(); M1 is statically
binded.
A* a = new C; A.M1
a->M1(); M1 is statically
binded.
B* b = new A; Violate subclass substitution.
b->M2(); B is not a superclass of A.
A* a = new C; B.M2
a->M2(); M2 is dynamically
binded. The latest
implementation is in
B.
A* a = new B; B.M2
a->M2(); M2 is dynamically
binded. The latest
implementation is in
B.
A* a = new C; A.M1
a->M3(); B.M2
A* a = new B; A.M1
a->M3(); B.M2
B* b = new B; A.M1
b->M1(); Inherited from A
12