CS1020E Midterm 1314S2
CS1020E Midterm 1314S2
CS1020E Midterm 1314S2
SCHOOL OF COMPUTING
SEMESTER II AY2013/2014
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. 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.
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.
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:
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.
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).
3
7. What happens if the base and derived class contains definition of a function with
same prototype?
#include <iostream>
using namespace std;
template<typename T>
class clsTemplate {
public:
T value;
clsTemplate(T i){
this->value = i;
}
void test() {
cout << value << endl;
}
};
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;
//-----------------------------------------
//-----------------------------------------
//-----------------------------------------
int main() {
Bird tweety;
Goose ralph;
Duck donald;
tweety = donald;
tweety.noise( );
donald.noise( );
ralph.noise( );
return 0;
}
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;
};
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;
}
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
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.
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)
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;
};
11
- Extra page if needed -
12