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

COMP103 Computer and Programming Fundamentals II
Spring 2006
Midterm Exam
Solution

April 6 2006




Student Name :




Email Address :


Student ID :


Lab Section : 1A 1B 1C





Instructions:

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


2.
Check that you have all 13 pages.


3.
Answer all questions directly in this Exam Book


4.
All work should be done inside this Exam Book





Question
You got
Full marks

Q1

26

Q2

18

Q3

10

Q4

9

Q5

21

Q6

16





Total

100







1.[ 26 marks] Show the output from the following program segment:
a) (6 marks)
int a = 100, b = 88, c = 8;
int *p1 = &a, *p2, *p3 = &c;
p2 = &b;
p2 = p1;
b = *p3;
*p2 = *p3;
cout << a << << b << << c;

Output:8 8 8
________________________________________________________________


b)
( 6 points )



#include <iostream.h>
int main() {
int x[2][3] = {{4, 5, 2},{7 , 6, 9}};
int (*p)[3] = &x[1];
int (*q)[3] = x;
cout << (*p)[0] << << (*p)[2] << ;
cout << (*q)[0] << << (*q)[1] << ;
cout << *(*(x+1)+2) << << *(*(x)+2) << endl;
return 0;
}Answer:7 9 4 5 9 2

___________________________________________________

c. [8 marks] Show the output from the following program segment:
int main(){
int a[5] = {2,4,6,8,22};
int *p = &a[3];
cout << a[0] << " " << p[0] << endl;
cout << a[1] << " " << p[-2] << endl;
cout << a[2] << " " << *(p+1 ) << endl;

return 0;}
Output:
2 8
_________________________________________________________
4 4
____________________________________________________________
6 22
____________________________________________________________
d) ( 6 marks) void sumLarger(const int a[], const int b[], int size, int* sum){
for (int i=0; i<size; i++){
if (a[i]>b[i])
*sum += a[i];
else
*sum += b[i]; }
}
int main() {
int a[5] = {2,10,4,12,6};
int b[5] = {0,3,6,9,12};
int sum = 0;
int* p = &sum;
sumLarger(a,b,5,p);
*p = *p + 8;
cout << sum << endl;
return 0;
}
Output: 50
2.[ 18 marks] Show the output from the following program:
a. [6marks]
#include <iostream.h>
int x,y;
void Fun1(int* a, int* b, int* c){
*a=*b+1;
*b=*c++ +2;
}
void Fun2(int a, int b, int c){
a=b+10;
b=c-- +20;
}
void Fun3(int& a,int& b,int& c){
a=b+1 ;
b=c++ +2 ;
}
int main()
{
x=2,y=5;
Fun1(&y,&x,&y);
cout << x << " " << y << endl;
x=2,y=5;
Fun2(y, y, x);
cout<< x << " " << y << endl;
x=2,y=5;
Fun3(x, y, y);
cout<< x << " " << y << endl;
return 0;
}

Output