{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

T09_mt_qtn

# T09_mt_qtn - THE HONG KONG UNIVERSITY OF SCIENCE TECHNOLOGY...

This preview shows pages 1–5. Sign up to view the full content.

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 – 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, efficiency, 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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
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? 2
(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 (int i = 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)? 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2. Basic OOP: A Ring (30 points total) We can construct a ring structure by connecting a linked list’s 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_head always 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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}