=========================preview======================
(comp152)[2009](s)final~1309^_10143.pdf
Back to COMP152 Login to download
======================================================
THE HONG KONG UNIVERSITY OF SCIENCE & TECHNOLOGY
Department of Computer Science & Engineering

COMP 152: Object-Oriented Programming and Data Structures
Fall 2009

Final Examination
Instructor: Gary Chan
Date: Thursday, December 17, 2009 Time: 8:30am C 11:30am Place: G017

.
Your answers will be graded according to correctness, ef.ciency, precision, and clarity.

.
This is a closed book examination. Put aside all your calculators, cellphones, PDAs, Palms, etc.

.
This booklet has 31 single-sided pages. Please check it.

.
You may use the reverse side of the pages for your rough work or continuation of work.


Student name: English nickname (if any): Student ID: Email address:
I have not violated the Academic Honor Code in this examination (your signature):


Question Your score
Maximum score

Question
Your score
Maximum score






1

5
5

20
2
12
6
10
3
10
7
13
4
7
8
13

Total
90

1. Short Questions (5 points, 1 point each)
(a) Is there any problem with the following function? If so, please state it.
int foo(){
int* a;
a = new int[2];
a[0] = 0;
a[1] = 1;
return 0;

}

(b) For the following program, if there is compilation or run-time error, brie.y explain the reason; otherwise, state its output.
#include <iostream>
using namespace std;

int main(){

int* ptr = NULL;

*ptr = 1;

cout << *ptr;
return 0;

}

(c) For the following program, if there is compilation or run-time error, brie.y explain the reason; otherwise, state its output.
#include <iostream>
using namespace std;

class A{
public:
A(){

str = "abc";
}
void printStrThenClear() const{

cout<<str;
str = "";
};
private:
string str;
};

int main(){
A foo;
foo.printStrThenClear();
return 0;

}

(d)
All but one of the followings must be a member function of a class. Please circle it.

i. ()
ii. []
iii. +=
iv. ==

(e)
Of the following, please circle the one which is used to solve the problem of variables with the same name and overlapping scopes.


i. Dynamic memory allocation
ii. Class
iii. Template
iv. Namespace
3


2. Basic OOP: Matrix (12 points)
You are to implement a 2-D array container class called Matrix. The container class has _mrows and _ncolumns (and hence totally _m x _n entries), all indexed from 0. You are to implement Matrixusing an internal 2D array _data. The [i, j]th entry is stored in _data[i][j], where 0 <= i <= _m-1and 0 <= j <= _n-1. The class is de.ned below:
template <class T>
class Matrix{
public:

Matrix(unsigned int m, unsigned int n); //Constructor
Matrix(const Matrix& m); //Copy Constructor
.Matrix(); //Destructor

const T& getValue(unsigned int m, unsigned int n) const;
// Get the value stored at the m-th row and n-th column

bool setValue(const T & value, unsigned int m, unsigned int n);
// Set the value at the m-th row and n-th column

private:
unsigned int _m,