=========================preview======================
(COMP151)exam.pdf
Back to COMP151 Login to download
======================================================
Hong Kong University of Science and Technology
COMP151: Object-Oriented Programming
Fall 2002
Final Examination
20 December 2002, 8:30-11:00am


Student Name: _________________________

Student Number: _________________________

Lab Section: _________________________



Instructions

1. This is a closed-book, closed-notes examination.

2. Check that you have all 17 pages (including this cover page).

3. Write your name, student number, and lab section on this page.

4. Answer all questions in the space provided.

5. Rough work should be done only on the back pages.




Question 1 (6%): __________________________

Question 2 (6%): __________________________

Question 3 (6%): __________________________

Question 4 (4%): __________________________

Question 5 (6%): __________________________

Question 6 (7%): __________________________

Question 7 (9%): __________________________

Question 8 (6%): __________________________

Question 9 (8%): __________________________

Question 10 (12%): __________________________

Question 11 (20%): __________________________

Question 12 (10%): __________________________


TOTAL (100%): __________________________

1. [6%] Given the following variable definitions:



int a[10];
int* p;

Circle the statements that are incorrect:

(a) p = a;

(b) p = &a;

(c) p = a[0];

(d) p = &a[0];

(e) p = &a[10];

(f) a = p;








2. [6%] What is the output produced when the following program is compiled and executed?



#include <iostream>
using namespace std;

int main()
{
int *p1, *p2, x = 3;

p1 = new int;
*p1 = x * 20;
p2 = p1;
cout << "A: " << x * *p1 << " " << *p2 << endl;
*p2 = 30;
cout << "B: " << x * *p1 << " " << *p2 << endl;
p1 = new int;
*p1 = 40;
cout << "C: " << x * *p1 << " " << *p2 << endl;

return 0;
}


3. [6%] Suppose the following function is defined to increment any specified integer by 1.



int* f(int* x)
{
(*x)++;
return x;
}

An integer variable a is defined and initialized as follows:

int a = 0;

(a) Using f() in one single statement, increment the variable a by 2.

(b) Define another function, called g(), that behaves similarly as f() using references rather than pointers.

(c) Using g() in one single statement, increment the variable a by 2.

























4. [4%] The following program is erroneous:



#include <iostream>
using namespace std;

double max(double a, double b)
{
return (a > b ? a : b);
}

double max(int a, int b)
{
return (a > b ? a : b);
}

int main()
{
cout << max(1.1, 2.2) << endl;
cout << max(1, 2.2) << endl;

return 0;
}


Which statement(s) cause(s) th