=========================preview======================
(COMP103)final04.pdf
Back to COMP103 Login to download
======================================================
HONG KONG UNIVERSITY OF SCIENCE AND TECHNOLOGY
COMP103 Computer and Programming Fundamentals II
Spring 2004
Final Exam Solution
May 22, 2004 (Sat)
9:00 am to 11:00 am
LG4204
Student Name :
Email Address :
Student ID :
Lab Section : 1A 1B 1C 1D
Instructions:
1.
Please write your name, student ID, and e-mail address on this page.
2.
This is a closed-book, closed-note examination.
3.
Check that you have all 13 pages.
4.
Answer all questions directly in this Exam Book
5.
All work should be done inside this Exam Book
6.
Full mark to this Exam is 70.
Question
Marks
Possible
Q1
5
Q2
5
Q3
5
Q4
10
Q5
10
Q6
5
Q7
10
Q8
10
Q9
10
Total
70
SHORT QUESTIONS
1. [5 marks] Given the following declarations
int a;
double b;
int* c;
double* d;
Indicate whether the following statement is correct or incorrect?
(Please circle your answer) [1 mark for each correct answer]
a.
b = a; Correct Incorrect
b.
a = c; Correct Incorrect
c.
c = &a; Correct Incorrect
d.
d = &a; Correct Incorrect
e.
*d = *c; Correct Incorrect
2. [5 marks] What is/are the problem(s) in the following recursive function?
// sum calculates the sum of the elements in a 1-D
// array, Obviously, this is not a good way to do so!!
int sum( int* array, int size)
{
int* tmp = new int[size-1];
for(int i = 0; i < size-1; i++)
tmp[i] = array[i];
return(array[size-1] + sum(tmp, size-1));
}
Answer:
No terminal case for the recursive function - 2.5 marks
Memory leak, no release (deletion) of memory allocated - 2.5marks
3. [5 marks] Consider the following link list, show the output from the following program segment: (Note: % is the mod operator, e.g. (7 % 2) = 1)
class Node{
public:
int data;
Node *next;
Node(int); // constructor
};
// function to print element
void odd(Node* in_node){
if(in_node == NUL)
return;
Node* pCur = in_node;
cout << pCur->data << ;
int i = (pCur->data % 2) + 1;
for(int j=0; j < i; j++)
if( pCur != NULL)
pCur = pCur->next;
odd(pCur);
return;
} // end recursive function odd
void main(){
// create the list, pHead is the pointer to
// the first node;
// call the function to print elements
odd(pHead);
} // end main
Output:
1 12 5 4 7 1 mark each
4. [10 marks]
A. [6 marks] Write the code for the following overloaded functions. The code should correctly exchange the values of a and b.
void exchange(int* a, int* b);
void exchange(int &a, int &b);
void exchange(int *a, int *b)
{