=========================preview======================
(COMP103)S2003-MidTerm-Exam (1).pdf
Back to COMP103 Login to download
======================================================
HONG KONG UNIVERSITY OF SCIENCE AND TECHNOLOGY

COMP103 Computer and Programming Fundamentals II
Spring 2003
Midterm Exam

April 7th, 2003

MIDTERM BOOK



Student Name :




Email Address :

Student ID :

Lecture Section: L1 L2

Lab Section : 1A / 1B (L1)

Lab Section : 2A/2B/2C (L2)





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

4

Q2

5

Q3

6

Q4

3

Q5

4

Q6

3

Q7

4

Q8

3

Q9

4

Q10

5

Q11

10

Q12

10

Total

60









SHORT QUESTIONS
Pointers and Dynamic Memory

1. [4pts] Given the following declarations
int x = 10;
int y = 20;
int *t = &x;
int **p = &t;
int ***q = &p;
which of the following expressions are incorrent?
(Please circle your answer [1pt each correct]
*t = **p + 1;

Correct
Incorrect

x = y + **q;

Correct
Incorrect

**p = ***q+1;

Correct
Incorrect

*q = &t;

Correct
Incorrect






2. [5 pts] The following piece of code has 5 cout statements. Write the output for each cout in the space provide. [1pts each cout statement]
void
main()
{
int a=5, b=10;
int *pi;
int **ppi;

pi = &a;
ppi = π
*pi = **ppi + 4; Write Output Here
cout << a << endl;

cout << *pi << endl;



*ppi = &b;
**ppi = b + 1;
cout << *pi << endl;

pi = &a;
*pi = **ppi + 2;

cout << a << endl;

cout << **ppi << endl;



}
3. [6pts] Consider the following variables and their associated memory addresses







*note, size of an integer is 4 bytes.
Please fill in the appropriate value in each memory cell, given the following code.
void main()
{
int x, y, z;
int *p, *q;
int **r;

x = 3;
p = &y;
q = &z;
r = &p;
**r = x + 9;
*q = *p + **r;
}


4 . [3pts] Given the following fragment of a program. Write different three ways in C++ you could assign a value of 5 to the second element in the array, a :

int a[10];
int *pa;
pa = &a[2];
Write answers here
(1)

(2)

(3)




x

y

z

r

100900 100900

100912

110024

120034

124800

140988

Memory Address
(in decimal)

Variables names

p

q


5. [4pts] Given the following fragment of a program:

int **data;
int rows=3, cols=4;
int i, j;
data = new int*[rows];
for (i=0; i<rows; i++)
{
data[i] = new int[cols];
for (j=0; j<cols; j++)
*(*(data+i)+j) = i + j;
}