=========================preview======================
(COMP152)[2010](s)midterm~1980^_10147.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
Spring 2010
Midterm Examination
Instructor: Gary Chan (L1) / Dit-Yan Yeung (L2) / Huamin Qu (L3)
Date: Saturday, 10 April 2010
Time: 10:00am C 11:45am
Venue: LTD (L1) / LTB (L2) / LTC (L3)
.
This is a closed-book examination. However, you are allowed to bring with you a piece of A4-sized paper with notes written or typed on both sides for use in the examination.
.
Your answers will be graded according to correctness, ef.ciency, precision, and clarity.
.
During the examination, you should put aside your calculators, mobile phones, PDAs and all other electronic devices. In particular, all mobile phones should be turned off completely.
.
This booklet has 10 single-sided pages. Please check that all pages are properly printed before you start working.
1. C++ Review (15 points total)
(a) [9 points] Consider the following C++ program which can be compiled without error:
#include <iostream>
using namespace std;
void swap1(int a, int b) {
int temp;
temp = a;
a= b;
b = temp;
}
void swap2(int a, int *b) {
int temp;
temp = a;
a = *b;
*b = temp;
}
void swap3(int *a, int& b) {
int *temp;
temp = a;
*a= b;
b = *temp;
}
int main() {
int x, y;
x= 10;y =20;
swap1(x, y);
cout<<x <<" "<< y << endl;
x= 10;y =20;
swap2(x, &y);
cout<<x <<" "<< y << endl;
x= 10;y =20;
swap3(&x, y);
cout<<x <<" "<< y << endl;
return 0;
}
What does it output?
(b) [6 points] Consider the following program which can be compiled without error:
#include <iostream>
using namespace std;
int main()
{
int *x = NULL;
int *y = NULL;
x = new int[3];
y = new int[3];
x= y;
for(inti= 1; i<= 3; i++)
{
x[i] = i;
}
delete x;
return 0;
}
The program contains one or more possible problem(s)/bug(s). What is/are the prob-lem(s)?
2. Basic OOP: A Ring (30 points total)
We can construct a ring structure by connecting a linked lists head with its tail (also known as a circular linked list). In this question, you are to implement a ring where insertion is always done at the beginning of the ring. Figure 1 shows an example of a ring with values 1, 2, 3 and 4 inserted sequentially. The pointer insert_headalways points to the beginning of the ring, which is the node most recently inserted.
Figure 1: A ring data structure with values 1, 2, 3 and 4 inserted sequentially.
You are given the header .le for the ring below:
// Ring Class Header File
#include <cstdlib>
#ifndef _RING
#define _RING
class RingNode {
public:
RingNode(const int& i = 0) : data(i), next(NULL) { }
private:
int data;
RingNode* next;
friend class Ring;
};
class Ring {
public:
Ring(); // default constructor
Ring(const Ring&); // copy constructor, no need to implement
.Ring(); // destructor
Ring& operator=(co