=========================preview======================
(COMP103)midterm04.pdf
Back to COMP103 Login to download
======================================================
HONG KONG UNIVERSITY OF SCIENCE AND TECHNOLOGY

COMP103 Computer and Programming Fundamentals II
Spring 2004
Midterm Exam
Solution

March 31, 2004
6:00 pm to 8:00 pm
LTA



Student Name :




Email Address :


Student ID :


Lab Section : 1A 1B 1C 1D





Instructions:

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


2.
Check that you have all 10 pages.


3.
Answer all questions directly in this Exam Book


4.
All work should be done inside this Exam Book


5.
The total marks possible is 60.





Question
Marks
Possible

Q1

5

Q2

3

Q3

4

Q4

4

Q5

6

Q6

4

Q7

4

Q8

5

Q9

15

Q10

10

Total

60







SHORT QUESTIONS
Pointers and Dynamic Memory

1. [5 marks] Given the following declarations
char c;
double d;
char *pc;
double *pd;
Indicate whether the following statement is correct or incorrect?
(Please circle your answer) [1 mark for each correct answer]
a.
c = C; Correct Incorrect


b.
pc = &c; Correct Incorrect


c.
d = 23.9; Correct Incorrect


d.
pd = &d; Correct Incorrect


e.
*pc = C; Correct Incorrect




2. [3 marks] Show the output from the following program segment:
int A[3][4] = {{1, 2, 3, 4},{5, 6, 7, 8},
{9, 10, 11, 12}};
int (*p)[4] = &A[1];
int (*q)[4] = A;
cout << (*p)[0] << " " << (*p)[2] << endl;
cout << (*q)[0] << " " << (*q)[1] << endl;
cout << *(*(A+2)+1) << " " << *(*(A)+1) << endl;
Output:
__________5 7__________________________________________________

__________1 2__________________________________________________

__________10 2__________________________________________________

____________________________________________________________

3. [4 marks] Show the output from the following program segment:
int num[5] = {3, 4, 6, 2, 1};
int *p = num;
int *q = num + 2;
int *r = &num[1];
cout << num[2] << << *(num+2) << endl;
cout << *p << << *(p+1) << endl;
cout << *q << << *(q+1) << endl;
cout << *r << << *(r+1) << endl;

Output:

__________6 6_________________________________________

__________3 4_________________________________________

__________6 2_________________________________________

__________4 6_________________________________________

___________________________________________________

4. [4 marks] Given the following program segment, write four different ways in C++ that you can assign the value of 10 to the fifth element in the array, Data:

int Data[10];
int *pa;
pa = &Data[9];

Your answer:

A.
_____Data[4] = 10;__________________________________




B.
_____*(Data+4) =10;________________________________




C.