=========================preview======================
(COMP104)[2008](f)final~2407^_10128.pdf
Back to COMP104 Login to download
======================================================
(For T.A. use only)
Number:



Lab section:





The Hong Kong University of Science and Technology
COMP104: Programming Fundamentals and Methodology

Fall 2008, Lecture Sections 1, 2, 3
Final Examination
Friday, December 12, 2008 12:30-15:30


Name: _____________________ Student ID: _____________________

Instructions:
1. This is a closed-book, closed-notes examination.
2. Write your name, student ID and email on this page.
(During the exam, a TA will pass by to check your ID and help fill the boxes at the top.)
3. Check that you have all 18 pages and additional 3 blank pages for rough work.
4. Answer all questions using the space provided after each question.
5. Rough work should be done on the last blank page provided.
6. All programming questions are in C++.

Problem
Score

1

/18

2

/16

3

/4

4

/10

5

/10

6

/10

7

/20

8

/12

Total

/100


Problem 1 [18 points]
Multiple-choice questions. Please circle the most appropriate answer. You can only pick one out of the four choices. (2 points per question)

(1) What is the problem associated to the following code?

#include <iostream>
using namespace std;
int main() {
int *p = new int;
int *q = p;
p = new int;
delete p;
delete q;
return 0;
}

(a) Compilation error
(b) Dangling pointer
(c) Memory leak
(d) None of above (Solution)

(2) What is the problem associated to the following code?

#include <iostream>
using namespace std;
int main() {
int *p = new int[100];
delete p;
return 0;
}

(a) Compilation error
(b) Dangling pointer
(c) Memory leak (Solution)
(d) None of above

(3) What is the problem associated to the following code?

#include <iostream>
using namespace std;
int main() {
int *p = new int[50000];
if ( p != NULL ) {
for (int i = 0; i < 50000; i++)
p[i] = i;
int *q = p;
delete [] p;
cout << *(q + 5000) << endl;
}
return 0;
}

(a) Compilation error
(b) Dangling pointer (Solution)
(c) Memory leak
(d) None of above

(4) Which pair of the following statements are equivalent?

(a) &(value[1]);
value + 1; (Solution)
(b) *&value;
*value;
(c) *value[2];
(*value++)++;
(d) *value;
&value;

(5) Which of the following function(s) can correctly swap two integers?

(I) void swap( int* p1, int* p2 ) { int *temp; *temp = *p1; *p1 = *p2; *p2 = *temp; }
(II) void swap( int& p1, int& p2 ) { int