=========================preview======================
(COMP102)Midterm 2000 Spring Solution.pdf
Back to COMP102 Login to download
======================================================
THE HONG KONG UNIVERSITY OF SCIENCE AND TECHNOLOGY
Department of Computer Science
COMP 102: Computer and Programming Fundamentals I
Spring 2001
Final Examination
Date: May 30 2001 Time: 4:30-6:30 pm Venue: Exposition Hall
This exam contains 11 questions in 16 pages. Please count the pages.
You have 2 hours to complete this exam.
Problem
Your points
Max points
Problem
Your points
Max points
1
7
7
10
2
6
8
9
3
8
9
20
4
4
10
10
5
8
11
12
6
6
Subtotal
39
Subtotal
61
Your total points
100
Please identify yourself: Lecture/Lab section
Name
Student id
Signature
1.
(7 points) The following program in the box prints out a multiplication table as shown below. Please fill in the missing codes to make it work properly.(The gaps between each colomns are made of 2 spaces.)
2x2= 4
2x3= 6 3x3= 9
2x4= 8 3x4=12 4x4=16
2x5=10 3x5=15 4x5=20 5x5=25
2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
#include <iostream.h>
#include <iomanip.h>
int main()
{
for (int i = 2; i <=9; i++ ){
for (int j=2; j <=i; j++ ){
cout << j <<x << i << =<<setw(2)<< i*j << ;
}
cout << endl;
}
}
2.) (6 points in total)
a) (3 points) What is the output to the screen?
int val = 0;
if (val = 0)
val = 103;
else val = 104;
cout << val ;
b) (3 points) What is the output to the screen?
int i = 7;
int foo(int &j)
{
j = i;
return j;
}
int main()
{
int i = 2, n;
n = foo(n) + i;
cout << n;
return 0;
}
3. (8 points) Euclid devised a clever algorithm for computing the greatest common divisor (GCD) of two integers. According to Euclid's algorithm,
104
9
GCD(M,N) =
Please write a recursive function GCD(M,N) to implement Euclids algorithm in the following box:
#include <iostream.h>
int GCD(int M, int N){
if (N>M) return GCD(N,M);
else if (N>0) return GCD(N, M%N);
else if (N== 0) return M;
}
int main() {
cout << GCD(54,36) << endl; //this line will output 18
return 0;
}
4)(4 points) What would be printed out on the screen by the following program?
#include <iostream.h>
int main(){
//local Declarations
int list[10] = {2,1,2,1,1,2,3,3,1,2};
//statement
cout << list[2] << endl;
cout << list[list[2]]<< endl;
cout <<list[list[2] + list[3]] << endl;
cout << list[list[list[2